Add failing test for variance measurement with nested generic type aliases #62934
+52
−0
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.
Summary
This PR adds a failing test case for issue #60453.
Bug: Variance is incorrectly computed as
Independent(instead ofCovariant) for type aliases with deeply nested anonymous object types containing Arrays. This causes type-unsafe assignments to be silently allowed.Investigation Findings
Reproduction
Root Cause Analysis
Variance Computation: In
getVariancesWorker(checker.ts:24958), variance is computed by comparingType<markerSubType>vsType<markerSuperType>usingisTypeAssignableTo.The Problem: For deeply nested types, both comparisons incorrectly return
true:isTypeAssignableTo(Sub, Super)=true(correct → Covariant)isTypeAssignableTo(Super, Sub)=true(INCORRECT → adds Contravariant)This results in
Bivariantvariance, which then becomesIndependentafter the subsequent check.Cache Evidence: After comparison, the relation cache shows:
SuperToSub = 1 (Succeeded)— wrong!SuperToSub = 2 (Failed)— correctOrder Dependency: The bug manifests only when the complex type is used first. If a simpler generic type with object structure (e.g.,
type Y<T> = { x: Array<T> }) is used before the complex type, the bug disappears.Potential Cause:
checkTypeRelatedToreturnsresult !== Ternary.False(line 22404). This meansTernary.Unknown(value 1) is treated astrue. When structural comparison encounters edge cases during first use,Unknownmay be incorrectly interpreted as success.Workaround
Explicit variance annotation fixes the issue:
Test Plan
varianceNestedGenericTypeAlias.tsFixes #60453