說我有一個功能:對象類型flowjs
const example = (item : ?{+type?: ?string}) : {+result: ?string} => {
const state = {}
state.result = item.result
return state
}
這失敗,給類型檢查:
12: state.result = item.result
^^^^^^^ property `result`. Property not found in
12: state.result = item.result
^^^^^^ object type
爲什麼沒有這種類型檢測?我沒有用確切的對象類型符號?{|+type?: ?string|}
來定義類型,所以它不應該允許其他鍵嗎?那麼確切的對象符號如何工作呢?我怎樣才能定義這樣的部分對象類型?這甚至有可能嗎?
您的參數'item'沒有名爲'result'的屬性。你是不是要編寫'state.result = item.type'呢? (儘管你還需要在那裏進行空檢查)。 –
如果我的問題不清楚,請隨時編輯。我的意思是這個對象中可能有很多鍵,並不是我可能會列舉的所有鍵,我不明白爲什麼流沒有使用「確切對象」符號來檢查類型。任何方式,我可以澄清這個問題(隨意編輯的方式!) –