You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR effects how we relate a source "Object" type to a target union type when:
The source is not relatable to a single type in the target union.
The target union has discriminant properties.
The source type would be sufficiently covered by all constituents of the union.
For example, today if we were to change IteratorResult to be a discriminated union, the following example would no longer compile:
typeIteratorResult<T,TReturn=T>=|{done: false,value: T}|{done: true,value: TReturn};classNumberIterator{next(){return{done: false,value: 1}}}letx: Iterator<number>=newNumberIterator();// error TS2322: Type 'NumberIterator' is not assignable to type 'IteratorResult<number>'.// Type '{ done: boolean, value: number }' is not assignable to type '{ done: true, value: number }'.// ...
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:
Generate a matrix of combinations of discriminant properties and types 'source' can satisfy.
If the number of combinations is above a fixed limit (currently 25), we consider the comparison too complex and determine that 'source' is not related to the 'target'.
Filter 'target' to the subset of constituents whose discriminants exist in the matrix.
If any combination of types in the matrix does not have a match in 'target', we determine 'source' is not related to 'target'.
For each matching constituent of 'target', we relate the remaining non-discriminant properties of 'source' to 'target'.
If any remaining properties are not related, we determine 'source' is not related to 'target'
Otherwise, 'source' is sufficiently covered by all constituents of 'target' and they are related.
This means that now, for the example above, we perform the following steps:
Heya @rbuckton, I've started to run the Definitely Typed test suite on this PR at 645853a. You can monitor the build here. It should now contribute to this PR's status checks.
Heya @rbuckton, I've started to run the extended test suite on this PR at 645853a. You can monitor the build here. It should now contribute to this PR's status checks.
Heya @rbuckton, I've started to run the perf test suite on this PR at c712918. You can monitor the build here. It should now contribute to this PR's status checks.
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.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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