2017-10-19 37 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