-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Relate source types covered by a target discriminated union #30779
Conversation
@typescript-bot test this |
@typescript-bot perf test |
Pardon the dust, tinkering with the on-demand benchmark script to not generate a massive wall of text when posting the results. |
04d985e
to
8321adf
Compare
Also #20889 |
8321adf
to
332ea0a
Compare
@RyanCavanaugh: I've added tests and verified that this does indeed fix those issues. |
@ahejlsberg: Can you take a look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd inline the constant instead of shouting all caps.
332ea0a
to
db7f701
Compare
db7f701
to
26fd6da
Compare
Hmm, why doesn't #12052 seem to link back to this issue? Oh, does locking an issue actually prevent it from getting new "@somebody mentioned this" links? That's too bad... it becomes much harder to follow up on these longstanding issues. |
This PR effects how we relate a source "Object" type to a target union type when:
For example, today if we were to change
IteratorResult
to be a discriminated union, the following example would no longer compile:With this change, we will perform the following steps after all other relationship checks fail:
If 'source' is an Object type and 'target' is a Union type with discriminant properties:
This means that now, for the example above, we perform the following steps:
S
with respect toT
:["done"]
[[true, false]]
[[true], [false]]
T
to the matching discriminants cm of c:[T0, T1]
["value"]
) ofS
and cm.S["value"]
is related to bothT0["value"]
andT1["value"]
, thereforeS
is related toT
.Further examples can be found in assignmentCompatWithDiscriminatedUnion.ts
Related: #2983
Fixes: #14865
Fixes: #30871
Fixes: #30170
Fixes: #12052
Fixes: #18421
Fixes: #15907
Fixes: #20889