This code:
type Action1 = {
name: 'A1',
}
type Action2 = {
name: 'A2',
}
type Action =
| Action1
| Action2;
function foo( body:Action):Action {
return {...body};
}
Produces this error:
14: return {...body};
^ Could not decide which case to select. Since case 1 [1] may work but if it doesn't case 2 [2] looks promising too. To fix add a type annotation to .name [3].
References:
10: | Action1
^ [1]
11: | Action2;
^ [2]
14: return {...body};
^ [3]
Replacing return {...body}; with return body fixes it. It doesn't seem like flow should have to decide on a member of the union just to use the spread operator?
Any progress on this? Is there a workaround annotation we could use in the meantime?
Spreads on objects are have a few issues at the moment and are being reworked (hopefully soon)
This doesn't really have to do with spread, you get the same error when you do the follow (which is essentially doing the same thing as spread):
function foo( body:Action):Action {
return { name: body.name };
}
Oh true, this is a separate bug.
Is there anything we can do to work around this atm? I'm also running into this issue and it is quite frustrating.
this will fix the issue: https://github.com/facebook/flow/pull/7298
This hurts me a bit... but seems to be a workaround:
type Action1 = {
name: 'A1',
};
type Action2 = {
name: 'A2',
};
type Action = Action1 | Action2;
function foo(body: Action): Action {
const flowIsBroken: any = { ...body };
const newlyTypedBody: Action = flowIsBroken;
return newlyTypedBody;
}
const shouldBeActionType = foo({ name: 'A1' });
// no errors means it's been typed correctly?
foo(shouldBeActionType);
This no longer errors in master