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

Union type can incorrectly fail when options have same-named but differently-typed property #1349

Closed
Macil opened this issue Feb 2, 2016 · 4 comments

Comments

@Macil
Copy link
Contributor

Macil commented Feb 2, 2016

If you have a union type where the first (by alphabetically) property of the first option has the same name as a property on a later option but with a different type, and then you have an object of the second type that has a value for that property which happens to also fit the type of the first option's first property, then Flow incorrectly gives a type error instead of realizing that the value belongs to the second option.

/* @flow */

var bar: Array<{b: ?boolean, c: number} | {b: boolean}> = [
  {b: true, c: 123},
  {b: true}
];
[chris /tmp/foo]$ flow version
Flow, a static type checker for JavaScript, version 0.21.0
[chris /tmp/foo]$ flow
bar.js:5
  5:   {b: true}
       ^^^^^^^^^ object literal. This type is incompatible with
  3: var bar: Array<{b: ?boolean, c: number} | {b: boolean}> = [
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ union: object type(s)


Found 1 error

The following changes each cause Flow to correctly not give any errors:

  • Re-ordering the union type.
var bar: Array<{b: boolean} | {b: ?boolean, c: number}> = [
  • Changing the first option's b property to match the type of the b property on the second option.
var bar: Array<{b: boolean, c: number} | {b: boolean}> = [
  • Changing the first option to have a required property alphabetically before b. ("c" -> "a" in the example.)
var bar: Array<{b: ?boolean, a: number} | {b: boolean}> = [
  {b: true, a: 123},
  {b: true}
];

I'm not familiar with Flow's own code, but I'd make a wild guess that Flow is incorrectly choosing not to backtrack after it sees a property in an object fit a property in an option and no further options have a property of the same exact type, even though further options might have a same-named property of a compatible type.

@avikchaudhuri
Copy link
Contributor

Yes, this is a long-standing known problem in the implementation of union types that we're working hard to fix. Your diagnosis is correct, btw.

@grncdr
Copy link
Contributor

grncdr commented Feb 18, 2016

Maybe this can be added #1331 ?

@Macil
Copy link
Contributor Author

Macil commented Feb 19, 2016

That's about intersection types, not union types. I don't know if this issue has much in common technically with those.

@avikchaudhuri
Copy link
Contributor

This will be fixed in an upcoming release.

@ghost ghost closed this as completed in 2df7671 Jun 10, 2016
This issue was closed.
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

4 participants