A false error (probably)
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
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
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?
Hmmm this is odd