Skip to content

Commit a3b3555

Browse files
TypeScript Botahejlsberg
TypeScript Bot
andauthored
Cherry-pick PR #50797 into release-4.8 (#50798)
Component commits: 23a1dd4 Fix bounds check 23784d7 Add regression test Co-authored-by: Anders Hejlsberg <[email protected]>
1 parent bb8cf90 commit a3b3555

5 files changed

+120
-4
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35931,8 +35931,8 @@ namespace ts {
3593135931
}
3593235932

3593335933
function getEffectiveTypeArgumentAtIndex(node: TypeReferenceNode | ExpressionWithTypeArguments, typeParameters: readonly TypeParameter[], index: number): Type {
35934-
if (index < typeParameters.length) {
35935-
return getTypeFromTypeNode(node.typeArguments![index]);
35934+
if (node.typeArguments && index < node.typeArguments.length) {
35935+
return getTypeFromTypeNode(node.typeArguments[index]);
3593635936
}
3593735937
return getEffectiveTypeArguments(node, typeParameters)[index];
3593835938
}

tests/baselines/reference/inferTypeConstraintInstantiationCircularity.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,26 @@ type MyObject<T> = T extends ZodObject<infer U>
7070
? U extends ZodRawShape
7171
? U
7272
: never
73-
: never;
73+
: never;
74+
75+
// Repro from #50479
76+
77+
type Cell<Value extends BaseValue = any, BaseValue = unknown> = {
78+
id: string
79+
}
80+
81+
type Items<Type extends Cell = Cell> = {
82+
type: Type
83+
name: string
84+
}
85+
86+
type InferIOItemToJSType<T extends Items> =
87+
T extends { type: infer U }
88+
? U extends Cell<infer V/**, infer _ or unknown, or any valid type **/>
89+
? V
90+
: never
91+
: never
92+
7493

7594
//// [inferTypeConstraintInstantiationCircularity.js]
7695
"use strict";

tests/baselines/reference/inferTypeConstraintInstantiationCircularity.symbols

+48
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,51 @@ type MyObject<T> = T extends ZodObject<infer U>
204204

205205
: never
206206
: never;
207+
208+
// Repro from #50479
209+
210+
type Cell<Value extends BaseValue = any, BaseValue = unknown> = {
211+
>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10))
212+
>Value : Symbol(Value, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 10))
213+
>BaseValue : Symbol(BaseValue, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 40))
214+
>BaseValue : Symbol(BaseValue, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 40))
215+
216+
id: string
217+
>id : Symbol(id, Decl(inferTypeConstraintInstantiationCircularity.ts, 75, 65))
218+
}
219+
220+
type Items<Type extends Cell = Cell> = {
221+
>Items : Symbol(Items, Decl(inferTypeConstraintInstantiationCircularity.ts, 77, 1))
222+
>Type : Symbol(Type, Decl(inferTypeConstraintInstantiationCircularity.ts, 79, 11))
223+
>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10))
224+
>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10))
225+
226+
type: Type
227+
>type : Symbol(type, Decl(inferTypeConstraintInstantiationCircularity.ts, 79, 40))
228+
>Type : Symbol(Type, Decl(inferTypeConstraintInstantiationCircularity.ts, 79, 11))
229+
230+
name: string
231+
>name : Symbol(name, Decl(inferTypeConstraintInstantiationCircularity.ts, 80, 12))
232+
}
233+
234+
type InferIOItemToJSType<T extends Items> =
235+
>InferIOItemToJSType : Symbol(InferIOItemToJSType, Decl(inferTypeConstraintInstantiationCircularity.ts, 82, 1))
236+
>T : Symbol(T, Decl(inferTypeConstraintInstantiationCircularity.ts, 84, 25))
237+
>Items : Symbol(Items, Decl(inferTypeConstraintInstantiationCircularity.ts, 77, 1))
238+
239+
T extends { type: infer U }
240+
>T : Symbol(T, Decl(inferTypeConstraintInstantiationCircularity.ts, 84, 25))
241+
>type : Symbol(type, Decl(inferTypeConstraintInstantiationCircularity.ts, 85, 13))
242+
>U : Symbol(U, Decl(inferTypeConstraintInstantiationCircularity.ts, 85, 25))
243+
244+
? U extends Cell<infer V/**, infer _ or unknown, or any valid type **/>
245+
>U : Symbol(U, Decl(inferTypeConstraintInstantiationCircularity.ts, 85, 25))
246+
>Cell : Symbol(Cell, Decl(inferTypeConstraintInstantiationCircularity.ts, 71, 10))
247+
>V : Symbol(V, Decl(inferTypeConstraintInstantiationCircularity.ts, 86, 26))
248+
249+
? V
250+
>V : Symbol(V, Decl(inferTypeConstraintInstantiationCircularity.ts, 86, 26))
251+
252+
: never
253+
: never
254+

tests/baselines/reference/inferTypeConstraintInstantiationCircularity.types

+31
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,34 @@ type MyObject<T> = T extends ZodObject<infer U>
101101
? U
102102
: never
103103
: never;
104+
105+
// Repro from #50479
106+
107+
type Cell<Value extends BaseValue = any, BaseValue = unknown> = {
108+
>Cell : Cell<Value, BaseValue>
109+
110+
id: string
111+
>id : string
112+
}
113+
114+
type Items<Type extends Cell = Cell> = {
115+
>Items : Items<Type>
116+
117+
type: Type
118+
>type : Type
119+
120+
name: string
121+
>name : string
122+
}
123+
124+
type InferIOItemToJSType<T extends Items> =
125+
>InferIOItemToJSType : InferIOItemToJSType<T>
126+
127+
T extends { type: infer U }
128+
>type : U
129+
130+
? U extends Cell<infer V/**, infer _ or unknown, or any valid type **/>
131+
? V
132+
: never
133+
: never
134+

tests/cases/compiler/inferTypeConstraintInstantiationCircularity.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,22 @@ type MyObject<T> = T extends ZodObject<infer U>
7070
? U extends ZodRawShape
7171
? U
7272
: never
73-
: never;
73+
: never;
74+
75+
// Repro from #50479
76+
77+
type Cell<Value extends BaseValue = any, BaseValue = unknown> = {
78+
id: string
79+
}
80+
81+
type Items<Type extends Cell = Cell> = {
82+
type: Type
83+
name: string
84+
}
85+
86+
type InferIOItemToJSType<T extends Items> =
87+
T extends { type: infer U }
88+
? U extends Cell<infer V/**, infer _ or unknown, or any valid type **/>
89+
? V
90+
: never
91+
: never

0 commit comments

Comments
 (0)