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 plains at the following:
type SomethingEmpty={||};
const x: SomethingEmpty = {};
Message is:
object literal. Inexact type is inpatible with exact type
This is not the same case as this one as no spread is used.
Tested with the latest 0.57.3
.
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 plains at the following:
type SomethingEmpty={||};
const x: SomethingEmpty = {};
Message is:
object literal. Inexact type is inpatible with exact type
This is not the same case as this one as no spread is used.
Tested with the latest 0.57.3
.
1 Answer
Reset to default 4The 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
Try
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
Try