Skip to content
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

Spreading to override null produces incorrect inferred object type #6357

Closed
leoselig opened this issue May 25, 2018 · 5 comments
Closed

Spreading to override null produces incorrect inferred object type #6357

leoselig opened this issue May 25, 2018 · 5 comments

Comments

@leoselig
Copy link

I was able to reproduce this error in a very minimal code snippet:

https://flow.org/try/#0PTAEAEDMBsHsHcBQjIFcB2BjALgS1uqGugBQAOATrGQM4CUoA3ogJCYE3agBuAhtLgAmALiZFYsUQH5OFXOgDmoAL6gAvGMgTR6VNGgAaUADpTlajRUBuRMuTESjcZNAAibAFNOrlXURA

While playing around it seemed to come down with flow inferring the type of the field foo to null and then regarding the spreaded string as a mismatch.

This can be fixed by reversing the order of spreads and default values like so:

https://flow.org/try/#0PTAEAEDMBsHsHcBQjIFcB2BjALgS1uqGugBQAOATrGQM4CUoA3ogJCYE3agBuAhtLgAmALiZFYsUQH5OFXOgDmoAL6gAvGIB02ytRoAacZNC7amyBNAAfK6HSpo0FQG5Ey5MRKMjogETYAU05fFTpEIA

Unfortunately, this comes at the cost of requiring the explicit listing of each potentially overriding field.

This is the most common error in our code base as it is basically showing up everytime when spreading different sets of props into React components in unit tests.

@chisui
Copy link

chisui commented May 26, 2018

For what it's worth your first example works if you use undefined instead of null. I couldn't find anthing in the docs that says those are treated differently in this case though.

@leoselig
Copy link
Author

@chisui Yes, I actually figured that out already. Since I understand ‘?’ to be the idiomatic way of expressing optional values without distinguishing between undefined and null I believe this should be addressed, am I correct?

@chisui
Copy link

chisui commented May 26, 2018

I think the problem is in the spread itself. It even occures if you use *, any or don't assign the object at all

This is the smallest example I could find of this behavior.

You can also fix this by explicitly casting null to ?string which shouldn't be needed

@villesau
Copy link
Contributor

I think this will fix the issue: #7298

@nmote
Copy link
Contributor

nmote commented Oct 25, 2019

This no longer errors on master

@nmote nmote closed this as completed Oct 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants