Fix spread operator failing to distribute over union when type is inlined#62836
Open
thromel wants to merge 1 commit intomicrosoft:mainfrom
Open
Fix spread operator failing to distribute over union when type is inlined#62836thromel wants to merge 1 commit intomicrosoft:mainfrom
thromel wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Author
|
@microsoft-github-policy-service agree |
Author
…ined Fixes microsoft#62812 When a union/intersection type like `number | string` appears 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 - different occurrences of `number | string` in the code were being treated as the same entity when they should be independent. The fix adds a check to skip this narrowing for structural types (unions/intersections) that don't contain type variables. Named types (interfaces, classes, etc.) still get narrowed since different references to the same named type refer to the same entity.
f44ce00 to
8d6b841
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.