该示例可以在@ 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, …Run Code Online (Sandbox Code Playgroud)