diff --git a/tests/cases/compiler/varianceNestedGenericTypeAlias.ts b/tests/cases/compiler/varianceNestedGenericTypeAlias.ts new file mode 100644 index 0000000000000..28ff31cd05c11 --- /dev/null +++ b/tests/cases/compiler/varianceNestedGenericTypeAlias.ts @@ -0,0 +1,52 @@ +// @strict: true + +// Repro from #60453 +// Variance measurement fails for type aliases with deeply nested anonymous object types +// containing Arrays, incorrectly computing variance as Independent instead of Covariant. + +type TypeA = number; +type TypeB = boolean; + +// Two levels of nesting - should be covariant in T +type DataTypeTwo = { + one: Array<{ two: Array }>; +}; + +// Three levels of nesting - should be covariant in T +type DataTypeThree = { + one: Array<{ two: Array<{ three: Array }> }>; +}; + +// Inline literal assignments correctly error +let testingThreeErr: DataTypeThree = { + one: [{ two: [{ three: [true] }] }] // Error expected +}; + +let testingTwoErr: DataTypeTwo = { + one: [{ two: [false] }] // Error expected +}; + +// Variable assignments should also error but currently don't due to +// variance being incorrectly computed as Independent +let testingThree: DataTypeThree = { + one: [{ two: [{ three: [5] }] }] +}; + +let t3a: DataTypeThree = testingThree; // Error expected: number not assignable to boolean + +let testingTwo: DataTypeTwo = { + one: [{ two: [5] }] +}; + +let t2a: DataTypeTwo = testingTwo; // Error expected: number not assignable to boolean + +// Workaround: explicit variance annotation fixes the issue +type DataTypeThreeFixed = { + one: Array<{ two: Array<{ three: Array }> }>; +}; + +let testingThreeFixed: DataTypeThreeFixed = { + one: [{ two: [{ three: [5] }] }] +}; + +let t3aFixed: DataTypeThreeFixed = testingThreeFixed; // Error expected and works correctly