Skip to content

Commit 5581fec

Browse files
committed
Add failing test for variance measurement with nested generic type aliases (#60453)
This test demonstrates incorrect variance computation for type aliases with deeply nested anonymous object types containing Arrays. The variance is incorrectly measured as Independent (meaning the type parameter is not used) instead of Covariant, causing type-unsafe assignments to be allowed.
1 parent f3770c9 commit 5581fec

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @strict: true
2+
3+
// Repro from #60453
4+
// Variance measurement fails for type aliases with deeply nested anonymous object types
5+
// containing Arrays, incorrectly computing variance as Independent instead of Covariant.
6+
7+
type TypeA = number;
8+
type TypeB = boolean;
9+
10+
// Two levels of nesting - should be covariant in T
11+
type DataTypeTwo<T extends TypeA | TypeB> = {
12+
one: Array<{ two: Array<T> }>;
13+
};
14+
15+
// Three levels of nesting - should be covariant in T
16+
type DataTypeThree<T extends TypeA | TypeB> = {
17+
one: Array<{ two: Array<{ three: Array<T> }> }>;
18+
};
19+
20+
// Inline literal assignments correctly error
21+
let testingThreeErr: DataTypeThree<TypeA> = {
22+
one: [{ two: [{ three: [true] }] }] // Error expected
23+
};
24+
25+
let testingTwoErr: DataTypeTwo<TypeA> = {
26+
one: [{ two: [false] }] // Error expected
27+
};
28+
29+
// Variable assignments should also error but currently don't due to
30+
// variance being incorrectly computed as Independent
31+
let testingThree: DataTypeThree<TypeA> = {
32+
one: [{ two: [{ three: [5] }] }]
33+
};
34+
35+
let t3a: DataTypeThree<TypeB> = testingThree; // Error expected: number not assignable to boolean
36+
37+
let testingTwo: DataTypeTwo<TypeA> = {
38+
one: [{ two: [5] }]
39+
};
40+
41+
let t2a: DataTypeTwo<TypeB> = testingTwo; // Error expected: number not assignable to boolean
42+
43+
// Workaround: explicit variance annotation fixes the issue
44+
type DataTypeThreeFixed<out T extends TypeA | TypeB> = {
45+
one: Array<{ two: Array<{ three: Array<T> }> }>;
46+
};
47+
48+
let testingThreeFixed: DataTypeThreeFixed<TypeA> = {
49+
one: [{ two: [{ three: [5] }] }]
50+
};
51+
52+
let t3aFixed: DataTypeThreeFixed<TypeB> = testingThreeFixed; // Error expected and works correctly

0 commit comments

Comments
 (0)