|
| 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