facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.08k stars 1.85k forks source link

Intersection should be commutative, is not. #4492

Open DanielHeath opened 7 years ago

DanielHeath commented 7 years ago

Example:

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCSGeAtmALxgDeAHgFxgD81AlgCaMDOGATqwDsA5gF8RmXAQAKvODi6UaYViVKNiZMCIDc6KAFcBAYwys4AsADE4cABQBKGqjAuwtnLPmMA8gCMAVngmYABkYDJyXI4UAHxgHpEAdCpkyey6OnqGJmYWAEIAhrwOTq5uCV7hngphfoEm0XEVXMmqaRm6QA

Expected: Intersecting two types would be commutative. Actual: Order matters.

mwiencek commented 7 years ago

I think I've experienced a similar issue trying to get certain mori types working in flow >= 0.23.

I have to reverse the order of the intersection there to get this line to type-check.

Try it here.

asolove commented 7 years ago

Hmm, this certainly isn't ideal, but I wonder what the right answer is. In this case, both Props and Object have (different) ways to look up .item, one of which (string property lookup) returns any and one of which returns a specific object type. I wonder how Flow should decide which one to use...

DanielHeath commented 7 years ago

There are a few situations where (imo) the right thing to do is clear - I'm unsure whether it would be more or less confusing to have flow pick the right one sometimes but guess or fail other times.

For instance, Foo & any is Foo; same for Object if Foo is a kind of object. Structural types are already doing the right thing IIRC.