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

Spread operator fails when deeply overriding objects #7135

Closed
pierpo opened this issue Nov 2, 2018 · 3 comments
Closed

Spread operator fails when deeply overriding objects #7135

pierpo opened this issue Nov 2, 2018 · 3 comments

Comments

@pierpo
Copy link

pierpo commented Nov 2, 2018

Hi,

I am trying to override an object with an enriched version of it using the spread operator but flow does not seem to understand.

Am I doing something wrong?

Example here

As you can see, the A and B types are the same, but the key user of the type B has one more field.
When I create a new object of type B by copying my object of type A with a spread operation and adding the new value through it, Flow gives an error.

// @flow

type A = {|
  stuff: string,
  user: {|
    name: string,
  |}
|}

type B = {|
  stuff: string,
  user: {|
    name: string,
    address: string,
  |},
|}

function tester(a: A): B {
  const b = {
    ...a,
    user: {
      ...a.user,
      address: 'hello'
    }
  };

  return b;
}

gives

27:   return b;
             ^ Cannot return `b` because property `address` is missing in object type [1] but exists in object type [2] in property `user`.
References:
5:   user: {|           ^ [1]
12:   user: {|            ^ [2]

Might be related to this issue.

Thanks for your help :)

@wchargin
Copy link
Contributor

wchargin commented Nov 4, 2018

Duplicate of #6108.

@pierpo
Copy link
Author

pierpo commented Nov 5, 2018

Indeed. Didn't find it, sorry about that 😉

@pierpo pierpo closed this as completed Nov 5, 2018
@wchargin
Copy link
Contributor

wchargin commented Nov 5, 2018

No problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants