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

Cannot override values when spreading #6108

Closed
villesau opened this issue Apr 6, 2018 · 7 comments
Closed

Cannot override values when spreading #6108

villesau opened this issue Apr 6, 2018 · 7 comments

Comments

@villesau
Copy link
Contributor

villesau commented Apr 6, 2018

Try flow link

type A = {
  a: number
};

type B = {
  a: string
};

const testFn = (input: A): B => ({...input, a: 'uh oh :/'});

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.

@vicapow
Copy link
Contributor

vicapow commented Apr 13, 2018

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'};

@villesau
Copy link
Contributor Author

I think it's clear from the code that the result type of a can't be number anymore and flow should be able to infer it as string. It just can't be number after spreading anymore.

@theres
Copy link

theres commented Jun 14, 2018

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!'});

@wchargin
Copy link
Contributor

wchargin commented Oct 5, 2018

@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, {a: 1, a: "won"} correctly has type {a: string} and not {a: number}.

@mrkev
Copy link
Contributor

mrkev commented Nov 28, 2018

Yup, looks like it's buggy

@villesau
Copy link
Contributor Author

villesau commented Dec 26, 2018

I managed to make a PR to fix this: #7298

@nmote
Copy link
Contributor

nmote commented Oct 25, 2019

This has been fixed and will typecheck in Flow v0.111.0

@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

7 participants