对象字面值。不精确类型与确切类型(没有对象扩散)
问题描述:
流量与确切类型工作正常的情况下以下不相容:对象字面值。不精确类型与确切类型(没有对象扩散)
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
。
答
的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
要无属性键入一个空Object
文字作为一个确切的类型,你需要明确地将其密封:
type Empty = {||};
const o :Empty = Object.seal({}); // type checks