-
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
Cannot override values when spreading #6108
Comments
You can override values when spreading it's just that when the two types differ, flow infers the type to be an union of the two. This seems like reasonable behavior to me. const a: { a: (number | string) } = {...{a: 10}, a: 'no error'}; |
I think it's clear from the code that the result type of |
Imho this should work. If you need to replace a few of fields out multiple, then you can use rest operator as a good engough workaround: type A = {
a: number,
b: string,
c: boolean
};
type B = {
a: string,
b: string,
c: boolean
};
const testFn = ({a, ...rest}: A): B => ({...rest, a: 'works!'}); |
@mrkev imho, this is not a feature request: it’s a bug. Flow’s understanding of the semantics of spread is simply incorrect, and inconsistent with its (correct) understanding of how field overriding works without spread: namely, |
Yup, looks like it's buggy |
I managed to make a PR to fix this: #7298 |
This has been fixed and will typecheck in Flow v0.111.0 |
Try flow link
As you can see, the property
a
is overridden with string in the function. Both, input and output are type checked, but flow complains that output and input are not compatible. This is very basic and idiomatic ES6 behaviour and should be fully supported.The text was updated successfully, but these errors were encountered: