0
該示例可在@flowtype.org/try找到。在這裏,我期望在兩個示例中使用有條件的類型細化,而它只適用於更簡單的一個。當我引入Array.filter
時,提煉不生效。這是Flow中的錯誤還是我的錯誤用法?通過過濾器擊敗的流聯合類型細化
/* @flow */
export type Action =
{| type: 'ACTION1', payload: string |}
| {| type: 'ACTION2', payload: number |}
| {| type: 'ACTION3' |}
const things = (state: Array<number> = [], action: Action): Array<number> => {
if (action.type === 'ACTION2') {
return state.filter((thing) => { return thing !== action.payload })
} else {
return state
}
}
things([1, 5], { type: 'ACTION2', payload: 5 })
const add = (state: number = 0, action: Action): number => {
if (action.type === 'ACTION2') {
return state + action.payload
} else {
return state
}
}
add(0, { type: 'ACTION2', payload: 5 })
生成以下錯誤:
10: return state.filter((thing) => { return thing !== action.payload })
^property `payload`. Property not found in
6: | {| type: 'ACTION3' |} ^object type