Skip to content

Commit 0dcaaa5

Browse files
committed
Simplify indexed accesses on mapped types with no depth limit
1 parent 02b13a9 commit 0dcaaa5

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ namespace ts {
6767
let enumCount = 0;
6868
let instantiationDepth = 0;
6969
let constraintDepth = 0;
70-
let simplificationDepth = 0;
7170

7271
const emptySymbols = createSymbolTable();
7372
const identityMapper: (type: Type) => Type = identity;
@@ -9687,31 +9686,15 @@ namespace ts {
96879686

96889687
// If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper
96899688
// that substitutes the index type for P. For example, for an index access { [P in K]: Box<T[P]> }[X], we
9690-
// construct the type Box<T[X]>. Mapped types can be recursive so to guard against infinite recursion we
9691-
// only perform this simplification up to five levels deep.
9692-
if (simplificationDepth < 5) {
9693-
if (isGenericMappedType(objectType)) {
9694-
return type.simplified = substituteIndexedMappedType(objectType, type);
9695-
}
9696-
if (objectType.flags & TypeFlags.TypeParameter) {
9697-
const constraint = getConstraintOfTypeParameter(objectType as TypeParameter);
9698-
if (constraint && isGenericMappedType(constraint)) {
9699-
return type.simplified = substituteIndexedMappedType(constraint, type);
9700-
}
9701-
}
9689+
// construct the type Box<T[X]>.
9690+
if (isGenericMappedType(objectType)) {
9691+
const mapper = createTypeMapper([getTypeParameterFromMappedType(objectType)], [type.indexType]);
9692+
const templateMapper = combineTypeMappers(objectType.mapper, mapper);
9693+
return type.simplified = mapType(instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper), getSimplifiedType);
97029694
}
97039695
return type.simplified = type;
97049696
}
97059697

9706-
function substituteIndexedMappedType(objectType: MappedType, type: IndexedAccessType) {
9707-
simplificationDepth++;
9708-
const mapper = createTypeMapper([getTypeParameterFromMappedType(objectType)], [type.indexType]);
9709-
const templateMapper = combineTypeMappers(objectType.mapper, mapper);
9710-
const result = mapType(instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper), getSimplifiedType);
9711-
simplificationDepth--;
9712-
return result;
9713-
}
9714-
97159698
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName, missingType = accessNode ? errorType : unknownType): Type {
97169699
if (objectType === wildcardType || indexType === wildcardType) {
97179700
return wildcardType;

0 commit comments

Comments
 (0)