Reputation: 27286
Flow works correctly with exact types in the below case:
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}; // --------||---------
… however Flow also complains at the following:
type SomethingEmpty={||};
const x: SomethingEmpty = {};
Message is:
object literal. Inexact type is incompatible with exact type
This is not the same case as this one as no spread is used.
Tested with the latest 0.57.3
.
Upvotes: 5
Views: 5897
Reputation:
The Object
literal without properties is inferred as an unsealed object type in Flow, this is to say you can add properties to such an object or deconstruct non-existing properties without errors being raised:
// 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
To type an empty Object
literal as an exact type without properties you need to seal it explicitly:
type Empty = {||};
const o :Empty = Object.seal({}); // type checks
Upvotes: 4