From 3b84f76fb23bd39d14c7243e5cd495fd207916c0 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 16 Sep 2022 07:14:14 -0700 Subject: [PATCH] Fix crash caused by incorrect bounds check (regression in 4.8) (#50797) * Fix bounds check * Add regression test --- src/compiler/checker.ts | 4 +- ...rTypeConstraintInstantiationCircularity.js | 21 +++++++- ...ConstraintInstantiationCircularity.symbols | 48 +++++++++++++++++++ ...peConstraintInstantiationCircularity.types | 31 ++++++++++++ ...rTypeConstraintInstantiationCircularity.ts | 20 +++++++- 5 files changed, 120 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9f38c49c50a31..32cee9afa06da 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -36070,8 +36070,8 @@ namespace ts { } function getEffectiveTypeArgumentAtIndex(node: TypeReferenceNode | ExpressionWithTypeArguments, typeParameters: readonly TypeParameter[], index: number): Type { - if (index < typeParameters.length) { - return getTypeFromTypeNode(node.typeArguments![index]); + if (node.typeArguments && index < node.typeArguments.length) { + return getTypeFromTypeNode(node.typeArguments[index]); } return getEffectiveTypeArguments(node, typeParameters)[index]; } diff --git a/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.js b/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.js index ca1488ed98615..e47b01ff47753 100644 --- a/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.js +++ b/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.js @@ -70,7 +70,26 @@ type MyObject = T extends ZodObject ? U extends ZodRawShape ? U : never - : never; + : never; + +// Repro from #50479 + +type Cell = { + id: string +} + +type Items = { + type: Type + name: string +} + +type InferIOItemToJSType = + T extends { type: infer U } + ? U extends Cell + ? V + : never + : never + //// [inferTypeConstraintInstantiationCircularity.js] "use strict"; diff --git a/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.symbols b/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.symbols index 4feca7bdc5e0f..10837383c8843 100644 --- a/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.symbols +++ b/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.symbols @@ -204,3 +204,51 @@ type MyObject = T extends ZodObject : never : never; + +// Repro from #50479 + +type Cell = { +>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10)) +>Value : Symbol(Value, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 10)) +>BaseValue : Symbol(BaseValue, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 40)) +>BaseValue : Symbol(BaseValue, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 40)) + + id: string +>id : Symbol(id, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 65)) +} + +type Items = { +>Items : Symbol(Items, Decl(inferTypeConstraintInstantiationCircularity.ts, 77, 1)) +>Type : Symbol(Type, Decl(inferTypeConstraintInstantiationCircularity.ts, 79, 11)) +>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10)) +>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10)) + + type: Type +>type : Symbol(type, Decl(inferTypeConstraintInstantiationCircularity.ts, 79, 40)) +>Type : Symbol(Type, Decl(inferTypeConstraintInstantiationCircularity.ts, 79, 11)) + + name: string +>name : Symbol(name, Decl(inferTypeConstraintInstantiationCircularity.ts, 80, 12)) +} + +type InferIOItemToJSType = +>InferIOItemToJSType : Symbol(InferIOItemToJSType, Decl(inferTypeConstraintInstantiationCircularity.ts, 82, 1)) +>T : Symbol(T, Decl(inferTypeConstraintInstantiationCircularity.ts, 84, 25)) +>Items : Symbol(Items, Decl(inferTypeConstraintInstantiationCircularity.ts, 77, 1)) + + T extends { type: infer U } +>T : Symbol(T, Decl(inferTypeConstraintInstantiationCircularity.ts, 84, 25)) +>type : Symbol(type, Decl(inferTypeConstraintInstantiationCircularity.ts, 85, 13)) +>U : Symbol(U, Decl(inferTypeConstraintInstantiationCircularity.ts, 85, 25)) + + ? U extends Cell +>U : Symbol(U, Decl(inferTypeConstraintInstantiationCircularity.ts, 85, 25)) +>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10)) +>V : Symbol(V, Decl(inferTypeConstraintInstantiationCircularity.ts, 86, 26)) + + ? V +>V : Symbol(V, Decl(inferTypeConstraintInstantiationCircularity.ts, 86, 26)) + + : never + : never + diff --git a/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.types b/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.types index 4571ec23357eb..ffdc2752dca62 100644 --- a/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.types +++ b/tests/baselines/reference/inferTypeConstraintInstantiationCircularity.types @@ -101,3 +101,34 @@ type MyObject = T extends ZodObject ? U : never : never; + +// Repro from #50479 + +type Cell = { +>Cell : Cell + + id: string +>id : string +} + +type Items = { +>Items : Items + + type: Type +>type : Type + + name: string +>name : string +} + +type InferIOItemToJSType = +>InferIOItemToJSType : InferIOItemToJSType + + T extends { type: infer U } +>type : U + + ? U extends Cell + ? V + : never + : never + diff --git a/tests/cases/compiler/inferTypeConstraintInstantiationCircularity.ts b/tests/cases/compiler/inferTypeConstraintInstantiationCircularity.ts index ad489da708312..6adf71624e28c 100644 --- a/tests/cases/compiler/inferTypeConstraintInstantiationCircularity.ts +++ b/tests/cases/compiler/inferTypeConstraintInstantiationCircularity.ts @@ -70,4 +70,22 @@ type MyObject = T extends ZodObject ? U extends ZodRawShape ? U : never - : never; \ No newline at end of file + : never; + +// Repro from #50479 + +type Cell = { + id: string +} + +type Items = { + type: Type + name: string +} + +type InferIOItemToJSType = + T extends { type: infer U } + ? U extends Cell + ? V + : never + : never