From 77d63f529a929a8676d813619e72b1e86ce0e4b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Fri, 11 Sep 2026 10:12:27 +0200 Subject: [PATCH 1/2] Fixed a crash related to variadic tuple elements with intersections containing `InstantiableNonPrimitive` --- tsc/internal/checker/checker.go | 11 ++++++++++- .../compiler/recursiveConditionalCrash5.errors.txt | 10 ++++++++++ .../compiler/recursiveConditionalCrash5.symbols | 13 +++++++++++++ .../compiler/recursiveConditionalCrash5.types | 8 ++++++++ .../cases/compiler/recursiveConditionalCrash5.ts | 6 ++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.types create mode 100644 tsc/testdata/tests/cases/compiler/recursiveConditionalCrash5.ts diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index ca77e97af8547..e3215baa00120 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -23707,7 +23707,9 @@ func (n *TupleNormalizer) normalize(c *Checker, elementTypes []*Type, elementInf if info.flags&ElementFlagsVariadic != 0 { if t.flags&TypeFlagsAny != 0 { n.add(t, TupleElementInfo{flags: ElementFlagsRest, labeledDeclaration: info.labeledDeclaration}) - } else if t.flags&TypeFlagsInstantiableNonPrimitive != 0 || c.isGenericMappedType(t) { + } else if someContainedType(t, func(t *Type) bool { + return t.flags&TypeFlagsInstantiableNonPrimitive != 0 || c.isGenericMappedType(t) + }) { // Generic variadic elements stay as they are. n.add(t, info) } else if isTupleType(t) { @@ -26893,6 +26895,13 @@ func everyType(t *Type, f func(*Type) bool) bool { return f(t) } +func someContainedType(t *Type, f func(*Type) bool) bool { + if t.flags&TypeFlagsUnionOrIntersection != 0 { + return core.Some(t.Types(), f) + } + return f(t) +} + func everyContainedType(t *Type, f func(*Type) bool) bool { if t.flags&TypeFlagsUnionOrIntersection != 0 { return core.Every(t.Types(), f) diff --git a/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.errors.txt b/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.errors.txt new file mode 100644 index 0000000000000..ac99cf2a6d542 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.errors.txt @@ -0,0 +1,10 @@ +recursiveConditionalCrash5.ts(3,60): error TS2589: Type instantiation is excessively deep and possibly infinite. + + +==== recursiveConditionalCrash5.ts (1 errors) ==== + // https://github.com/microsoft/TypeScript/issues/63040 + + type StringTreeArrayAsTuple = (T extends [...infer R] ? [...StringTreeArrayAsTuple] : never) & boolean; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2589: Type instantiation is excessively deep and possibly infinite. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.symbols b/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.symbols new file mode 100644 index 0000000000000..088577dcd7b43 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.symbols @@ -0,0 +1,13 @@ +//// [tests/cases/compiler/recursiveConditionalCrash5.ts] //// + +=== recursiveConditionalCrash5.ts === +// https://github.com/microsoft/TypeScript/issues/63040 + +type StringTreeArrayAsTuple = (T extends [...infer R] ? [...StringTreeArrayAsTuple] : never) & boolean; +>StringTreeArrayAsTuple : Symbol(StringTreeArrayAsTuple, Decl(recursiveConditionalCrash5.ts, 0, 0)) +>T : Symbol(T, Decl(recursiveConditionalCrash5.ts, 2, 28)) +>T : Symbol(T, Decl(recursiveConditionalCrash5.ts, 2, 28)) +>R : Symbol(R, Decl(recursiveConditionalCrash5.ts, 2, 53)) +>StringTreeArrayAsTuple : Symbol(StringTreeArrayAsTuple, Decl(recursiveConditionalCrash5.ts, 0, 0)) +>R : Symbol(R, Decl(recursiveConditionalCrash5.ts, 2, 53)) + diff --git a/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.types b/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.types new file mode 100644 index 0000000000000..671d71e3f6470 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/recursiveConditionalCrash5.types @@ -0,0 +1,8 @@ +//// [tests/cases/compiler/recursiveConditionalCrash5.ts] //// + +=== recursiveConditionalCrash5.ts === +// https://github.com/microsoft/TypeScript/issues/63040 + +type StringTreeArrayAsTuple = (T extends [...infer R] ? [...StringTreeArrayAsTuple] : never) & boolean; +>StringTreeArrayAsTuple : StringTreeArrayAsTuple + diff --git a/tsc/testdata/tests/cases/compiler/recursiveConditionalCrash5.ts b/tsc/testdata/tests/cases/compiler/recursiveConditionalCrash5.ts new file mode 100644 index 0000000000000..87641a5ca9277 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/recursiveConditionalCrash5.ts @@ -0,0 +1,6 @@ +// @strict: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/63040 + +type StringTreeArrayAsTuple = (T extends [...infer R] ? [...StringTreeArrayAsTuple] : never) & boolean; From ddd33e9be1b9bea455ba0733120f6bac9f123ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 18 Nov 2026 19:01:32 +0100 Subject: [PATCH 2/2] Test variadic tuple spreads with generic intersections --- .../variadicTupleSpreadIntersection.symbols | 34 +++++++++++++++++++ .../variadicTupleSpreadIntersection.types | 30 ++++++++++++++++ .../variadicTupleSpreadIntersection.ts | 11 ++++++ 3 files changed, 75 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.types create mode 100644 tsc/testdata/tests/cases/compiler/variadicTupleSpreadIntersection.ts diff --git a/tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.symbols b/tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.symbols new file mode 100644 index 0000000000000..077afcdff64e1 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.symbols @@ -0,0 +1,34 @@ +//// [tests/cases/compiler/variadicTupleSpreadIntersection.ts] //// + +=== variadicTupleSpreadIntersection.ts === +type Spread = [0, ...(T & readonly unknown[])]; +>Spread : Symbol(Spread, Decl(variadicTupleSpreadIntersection.ts, 0, 0)) +>T : Symbol(T, Decl(variadicTupleSpreadIntersection.ts, 0, 12)) +>T : Symbol(T, Decl(variadicTupleSpreadIntersection.ts, 0, 12)) + +type A = Spread<[1, 2]>; +>A : Symbol(A, Decl(variadicTupleSpreadIntersection.ts, 0, 77)) +>Spread : Symbol(Spread, Decl(variadicTupleSpreadIntersection.ts, 0, 0)) + +type Tagged = [0, ...(T & { tag?: 1 })]; +>Tagged : Symbol(Tagged, Decl(variadicTupleSpreadIntersection.ts, 1, 24)) +>T : Symbol(T, Decl(variadicTupleSpreadIntersection.ts, 3, 12)) +>T : Symbol(T, Decl(variadicTupleSpreadIntersection.ts, 3, 12)) +>tag : Symbol(tag, Decl(variadicTupleSpreadIntersection.ts, 3, 48)) + +type B = Tagged; +>B : Symbol(B, Decl(variadicTupleSpreadIntersection.ts, 3, 61)) +>Tagged : Symbol(Tagged, Decl(variadicTupleSpreadIntersection.ts, 1, 24)) + +declare function f(x: T): [0, ...(T & { tag?: 1 })]; +>f : Symbol(f, Decl(variadicTupleSpreadIntersection.ts, 4, 26)) +>T : Symbol(T, Decl(variadicTupleSpreadIntersection.ts, 6, 19)) +>x : Symbol(x, Decl(variadicTupleSpreadIntersection.ts, 6, 40)) +>T : Symbol(T, Decl(variadicTupleSpreadIntersection.ts, 6, 19)) +>T : Symbol(T, Decl(variadicTupleSpreadIntersection.ts, 6, 19)) +>tag : Symbol(tag, Decl(variadicTupleSpreadIntersection.ts, 6, 60)) + +const c = f([1, "a"] as [1, "a"]); +>c : Symbol(c, Decl(variadicTupleSpreadIntersection.ts, 7, 5)) +>f : Symbol(f, Decl(variadicTupleSpreadIntersection.ts, 4, 26)) + diff --git a/tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.types b/tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.types new file mode 100644 index 0000000000000..2e5c0e2ed456e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/variadicTupleSpreadIntersection.types @@ -0,0 +1,30 @@ +//// [tests/cases/compiler/variadicTupleSpreadIntersection.ts] //// + +=== variadicTupleSpreadIntersection.ts === +type Spread = [0, ...(T & readonly unknown[])]; +>Spread : [0, ...T & readonly unknown[]] + +type A = Spread<[1, 2]>; +>A : [0, ...(1 | 2)[]] + +type Tagged = [0, ...(T & { tag?: 1 })]; +>Tagged : [0, ...T & { tag?: 1; }] +>tag : 1 | undefined + +type B = Tagged; +>B : [0, ...string[]] + +declare function f(x: T): [0, ...(T & { tag?: 1 })]; +>f : (x: T) => [0, ...(T & { tag?: 1; })] +>x : T +>tag : 1 | undefined + +const c = f([1, "a"] as [1, "a"]); +>c : [0, ...("a" | 1)[]] +>f([1, "a"] as [1, "a"]) : [0, ...("a" | 1)[]] +>f : (x: T) => [0, ...(T & { tag?: 1; })] +>[1, "a"] as [1, "a"] : [1, "a"] +>[1, "a"] : [1, "a"] +>1 : 1 +>"a" : "a" + diff --git a/tsc/testdata/tests/cases/compiler/variadicTupleSpreadIntersection.ts b/tsc/testdata/tests/cases/compiler/variadicTupleSpreadIntersection.ts new file mode 100644 index 0000000000000..ee8a04ee87ad5 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/variadicTupleSpreadIntersection.ts @@ -0,0 +1,11 @@ +// @strict: true +// @noEmit: true + +type Spread = [0, ...(T & readonly unknown[])]; +type A = Spread<[1, 2]>; + +type Tagged = [0, ...(T & { tag?: 1 })]; +type B = Tagged; + +declare function f(x: T): [0, ...(T & { tag?: 1 })]; +const c = f([1, "a"] as [1, "a"]);