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

Flow ignores attribute overwritten with the spread operator (example included) #4045

Closed
typehorror opened this issue May 28, 2017 · 4 comments

Comments

@typehorror
Copy link

typehorror commented May 28, 2017

It seems that when the spread operator is being used to extend a value, flow somewhat can't identify the presence of a sub-attribute.

Problem is reproduced here on the flow try page

/* @flow */

type ManyFoobar = {
  foobar: {
    values: Array<string>
  }
};

type OneFoobar = {
  foobar: {
    value: string
  }
};

// Uses the logic of manyFoobarMethod by
// placing its single argument into an array
function oneFoobarMethod(param: OneFoobar) {
  const paramForMany = {
    ...param,
    foobar: {
      values: [param.foobar.value]
    }
  };
  return manyFoobarMethod(paramForMany);
}

function manyFoobarMethod(param: ManyFoobar) {
  return param.foobar.values.length;
}

oneFoobarMethod({ foobar: { value: "test" } });
manyFoobarMethod({ foobar: { values: ["test"] } });

triggers this error:

4:   foobar: {
             ^ property `values`. Property not found in
10:   foobar: {
              ^ object type
@agentcooper
Copy link
Contributor

Possible workaround:

function oneFoobarMethod(param: OneFoobar) {
  const { foobar, ...rest } = param;

  const paramForMany: ManyFoobar = {
    ...rest,
    foobar: {
      values: [param.foobar.value],
    },
  };

  return manyFoobarMethod(paramForMany);
}

@typehorror
Copy link
Author

typehorror commented May 28, 2017

@agentcooper it seems indeed that while your technique yields the same result, flow is inferring the value correctly and does not error out. Hopefully this gives more insight about the bug.

@typehorror typehorror changed the title Flow ignores attribute assigned with the spread operator (example included) Flow ignores attribute overwritten with the spread operator (example included) May 28, 2017
@villesau
Copy link
Contributor

This fixes the issue: #7298

@nmote
Copy link
Contributor

nmote commented Oct 25, 2019

This no longer errors in 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