2017-10-19 9 views
2

流れは以下の場合には、正確なタイプで正常に動作するとは互換性がありません:オブジェクトリテラル。不正確タイプは正確な型(なしオブジェクトスプレッド)

type Something={|a: string|}; 
const x1: Something = {a: '42'};  // Flow is happy 
const x2: Something = {};    // Flow correctly detects problem 
const x3: Something = {a: '42', b: 42}; // --------||--------- 

…

type SomethingEmpty={||}; 
const x: SomethingEmpty = {}; 

メッセージは次のとおりです:何のスプレッドが使用されていないよう

object literal. Inexact type is incompatible with exact type 

これはthis oneと同じケースではありません。しかし、次の文句も流れ

最新の0.57.3でテスト済みです。

答えて

1

性質なしリテラルObjectが、これはあなたがそのようなオブジェクトにプロパティを追加したり、エラーが発生することなく、非既存のプロパティを分解できると言うことです、フローでシールされていないオブジェクト型として推論されます。

// inferred as... 

const o = {}; // unsealed object type 
const p = {bar: true} // sealed object type 

const x = o.foo; // type checks 
o.bar = true; // type checks 

const y = p.foo; // type error 
p.baz = true; // type error 

Try

プロパティずに正確な型として空Objectリテラルを入力するには、明示的にそれを封印する必要があります。

type Empty = {||}; 
const o :Empty = Object.seal({}); // type checks 

Try