-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Intersecting discriminated union breaks type narrowing #9919
Comments
Sounds like you want the compiler to be aware of the distributive law: T & (U | V) ≡ (T & U) | (T & V) |
Got caught by this today. I was unsure if it was too far-fetched to open an issue or not ;) @yortus I don't think about it as "distributive" law, although I guess you can see it that way. The tricky part with seeing it as "distributive" is that it may explode quickly, consider: |
The distributive law is just the 'set theory' principle that makes it valid. Whether the compiler expands all the terms or not is implementation detail, I guess with tradeoffs either way. As you point out it could create an explosion of types. On the other hand, doing a full expansion would allow complex types to be 'normalized', so you could tell when they are identical, eliminate redundant terms, etc. |
@yortus I'm nitpicking here: I think what the compiler is lacking is the distributivity of the narrowing operation. Because it doesn't seem to narrow intersection types and that's the problem in It seems what the compiler lacks is knowledge that, if |
This issue is about narrowing. I think we are saying the same thing. |
Is here some progress? I've checked a lot of discussions, but haven't found any decision. Would be distributive law for unions and intersections implemented? And if yes, in what release we can expect that? |
Fix now available in #11717. |
Great news! @yortus turned out to be right! :) (in implementation sense, in logical there has never been any doubt) |
TypeScript Version: nightly (2.0.0-dev.201xxxxx)
Code
If a discriminated union is enhanced via intersection, type narrowing based on the discrimination field starts to fail. This behaviour is not exhibited if a new discriminated union is created with the intersection being inlined into each of the respective options.
The function
getValueAsString(value: IntersectionFail): string
fails to compile, whilegetValueAsString(value: IntersectionInline): string
compiles just fine. Unfortunately, it isIntersectionFail
that is the result of natural program evolution.The text was updated successfully, but these errors were encountered: