Skip to content

Commit 709f5f2

Browse files
committed
Handle circular mapped type instantiations for arrays and tuples
1 parent 6cdba6d commit 709f5f2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10365,7 +10365,15 @@ namespace ts {
1036510365
if (typeVariable) {
1036610366
const mappedTypeVariable = instantiateType(typeVariable, mapper);
1036710367
if (typeVariable !== mappedTypeVariable) {
10368-
return mapType(mappedTypeVariable, t => {
10368+
// If we are already in the process of creating an instantiation of this mapped type,
10369+
// return the error type. This situation only arises if we are instantiating the mapped
10370+
// type for an array or tuple type, as we then need to eagerly resolve the (possibly
10371+
// circular) element type(s).
10372+
if (type.instantiating) {
10373+
return errorType;
10374+
}
10375+
type.instantiating = true;
10376+
const result = mapType(mappedTypeVariable, t => {
1036910377
if (t.flags & (TypeFlags.AnyOrUnknown | TypeFlags.InstantiableNonPrimitive | TypeFlags.Object | TypeFlags.Intersection) && t !== wildcardType) {
1037010378
const replacementMapper = createReplacementMapper(typeVariable, t, mapper);
1037110379
return isArrayType(t) ? createArrayType(instantiateMappedTypeTemplate(type, numberType, /*isOptional*/ true, replacementMapper)) :
@@ -10375,6 +10383,8 @@ namespace ts {
1037510383
}
1037610384
return t;
1037710385
});
10386+
type.instantiating = false;
10387+
return result;
1037810388
}
1037910389
}
1038010390
return instantiateAnonymousType(type, mapper);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,6 +4074,7 @@ namespace ts {
40744074
templateType?: Type;
40754075
modifiersType?: Type;
40764076
resolvedApparentType?: Type;
4077+
instantiating?: boolean;
40774078
}
40784079

40794080
export interface EvolvingArrayType extends ObjectType {

0 commit comments

Comments
 (0)