flow icon indicating copy to clipboard operation
flow copied to clipboard

A false error (probably)

Open eddig opened this issue 9 years ago • 3 comments

I took an example from here and made changes in the definition of State type and in reducer's cases. (reproduced on Try Flow)

/* @flow */

type State = Array<boolean>;

type FooAction = { type: 'FOO', foo: boolean };
type BarAction = { type: 'BAR', bar: boolean };

type Action = FooAction | BarAction;

function reducer(state: State, action: Action): State {
  switch (action.type) {
    case 'FOO':
      return [...state, action.foo];
    case 'BAR':
      return state.filter(v => v !== action.bar);
    default:
      return state;
  }
}

export default reducer;

This code works as expected, but the Flow shows error about missing property bar in action.

15:       return state.filter(v => v !== action.bar);
                                                ^ property `bar`. Property not found in
15:       return state.filter(v => v !== action.bar);
                                         ^ object type

eddig avatar Jul 10 '17 12:07 eddig

the problem is that refinements are pessimistic, flow cannot assume that action is still a BarAction (so it might now have the bar property). Here's a quick workaround (store the refined action in a const variable):

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDKGAhhgQLxgCCATrSVgDwBGccMeJAdgHwDc6bPjAAxdtQDGGAJZxuYKgG8wwvAC4wAclEB5XVoA0YKO01sOXBQF9BasACEStKbPmKwKtZq0PqAJSMwFmdzdk4eMFshXAJXOQUqcTh49wAfR2dU7kFUKABXbmkEsFo8ABN8yTxaAAoAZ1JyTWIyPGMSYvlNbIBKFqaCJVQwMHqEGQxJAAswWs63bgA6NV7PEdGwSRJ6gh19LXUNzdK8DHzaBQBtJdvGto6u5dM4AF1BE+3d7T9Ao5PRpJ5I0wAB9BYlKgQ+QfAFlc6XMaDJZQGQwch1ABuil4YGxAEIKFRwU8liFaL1YaNyngoCR8uj-nCzhcFPdyLDrKguag8AAPHBwWgYMA0ukMkVlSrVWiCIA

madbence avatar Jul 10 '17 14:07 madbence

Thank you @madbence, it works. But looks ugly. If it is a workaround, then we have any chance that it would be work as expected in a near future?

eddig avatar Jul 13 '17 06:07 eddig

Hmmm this is odd

mrkev avatar Jun 08 '18 23:06 mrkev