From 5581fece547239f1b45f43b53ca168be2d39d08a Mon Sep 17 00:00:00 2001 From: Pavel Piuro Date: Wed, 31 Dec 2025 00:01:51 +0400 Subject: [PATCH] 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. --- .../varianceNestedGenericTypeAlias.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/cases/compiler/varianceNestedGenericTypeAlias.ts 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