diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 859638d5970a0..f2436c07c018e 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -24197,7 +24197,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) { @@ -27418,6 +27420,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/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/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; 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"]);