javascript - Flow union type refinement defeated by filter -


the example can found @ flowtype.org/try. here expect type refinement in conditional work in both examples, while works in simpler one. when introduce array.filter refinement not take effect. bug in flow or mis-usage on part?

/* @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 }) 

generates following errors:

10:     return state.filter((thing) => { return thing !== action.payload })                                                                  ^ property `payload`. property not found in 6:   | {| type: 'action3' |}       ^ object type 

this matter of flow aggressively invalidating type refinements. flow not know filter going callback pass. maybe it's going save , call later. flow doesn't realize nothing else reassigns action. far it's concerned, action might reassigned {type: 'action3'} time callback called. pulling payload out const solves issue:

const payload = action.payload; return state.filter((thing) => { return thing !== payload }) 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -