-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using spread operator to create a new type with possibly undefined property creates an object where either of the property values are possible #6526
Comments
It does work as expected when the properties are not optional, e.g. type UnnormalizedEventType = {|
name: 'foo'
|};
type EventType = {|
...UnnormalizedEventType,
name: 'bar'
|};
const event: EventType = ({}: any);
const test = (name: 'bar') => {};
test(event.name); How to make it work with optional properties? |
You can workaround this with |
ping @samwgoldman @panagosg7 @nmote |
ping @gabelevi can this be marked as a bug? |
@gajus - I think the idea here is for object type spread to mimic object spread. So you in theory should be able to type something like
My guess is that the object spread in your example kind of works like
And since But yeah, as you wrote it we probably could just override the previous value. Let me ask @samwgoldman if I'm missing some edge case. |
Yep! That's exactly what's happening...to make the implementation simpler we were turning the inline properties into another spread. But we totally could make the inline properties always override. I'll mark this as a bug! |
@gabelevi Keep hitting this issue over and over. :-( @samwgoldman any news on this? The only quasi-useful solution I have found is to create a custom utility as such: |
This now works |
https://flow.org/try/#0C4TwDgpgBAqgdnA9gJwLYEMA2BLAXhAEwFEA3COYAFXGgF4oBvAHwCgoo51UIB+ALigByAGaJEglkwC+AbhYtQkKKXJUaUeszZQAdHvhI0WPIRUVqkADTbO3fkIBG6ZBOlyWAY0RwAzsCgQZBQCZmpK9AAUDFIC6HAgAJTuXr7+wBB+GlARtrwCgk4uCRoAfIyy8ul+EYGqOrlJQA
Expectation: No error.
Actual:
My understanding is that
EventType
should become:Though instead it appears to be treated as:
Is this a bug/ feature?
If this is a feature, then how do I spread-copy an object without creating union of both object property types and just keeping the latest.
The text was updated successfully, but these errors were encountered: