Fix spread operator failing to distribute over union when type is inlined #62836
+436
−2
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.
Fixes #62812
Problem
When a union/intersection type like
number | stringappears in the true branch of a conditional type that has the same type as its check type, the type was incorrectly being narrowed with a substitution constraint. This caused issues when the union type was used as a type argument to a generic type.For example:
Root Cause
In
getConditionalFlowTypeOfType, when processing a type node in the true branch of a conditional type, the function checks if the type matches the conditional's check type and creates a substitution type with the implied constraint.The issue is that for structural types like
number | string, different occurrences in the code all resolve to the same canonical type. So whenCrossProduct<number | string, [undefined]>appeared inside the true branch ofnumber | string extends infer Union ? ..., the type argumentnumber | stringwas incorrectly being narrowed with the constraintUnioneven though it was a completely independent occurrence.Fix
Added a check to skip this narrowing for union/intersection types that don't contain type variables:
This ensures:
Test
Added
tests/cases/conformance/types/spread/spreadTupleUnionDistribution.tsto verify the fix.