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

Projects
None yet
4 participants
@Macil
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

This comment has been minimized.

Show comment
Hide comment
@avikchaudhuri

avikchaudhuri Feb 3, 2016

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.

Contributor

avikchaudhuri commented Feb 3, 2016

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

This comment has been minimized.

Show comment
Hide comment
@grncdr

grncdr Feb 18, 2016

Contributor

Maybe this can be added #1331 ?

Contributor

grncdr commented Feb 18, 2016

Maybe this can be added #1331 ?

@Macil

This comment has been minimized.

Show comment
Hide comment
@Macil

Macil Feb 19, 2016

Contributor

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

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@avikchaudhuri

avikchaudhuri Jun 1, 2016

Contributor

This will be fixed in an upcoming release.

Contributor

avikchaudhuri commented Jun 1, 2016

This will be fixed in an upcoming release.

@ghost ghost closed this 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