Skip to content

Commit fd8c757

Browse files
committed
Constraints for mapped types with filtering 'as' clauses
1 parent a027cfa commit fd8c757

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15737,11 +15737,14 @@ namespace ts {
1573715737
return type[cache] = elementType;
1573815738
}
1573915739
}
15740-
// If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper
15741-
// that substitutes the index type for P. For example, for an index access { [P in K]: Box<T[P]> }[X], we
15742-
// construct the type Box<T[X]>.
15743-
if (isGenericMappedType(objectType) && !objectType.declaration.nameType) {
15744-
return type[cache] = mapType(substituteIndexedMappedType(objectType, type.indexType), t => getSimplifiedType(t, writing));
15740+
// If the object type is a mapped type { [P in K]: E }, where K is generic, or { [P in K as N]: E }, where
15741+
// K is generic and N is assignable to P, instantiate E using a mapper that substitutes the index type for P.
15742+
// For example, for an index access { [P in K]: Box<T[P]> }[X], we construct the type Box<T[X]>.
15743+
if (isGenericMappedType(objectType)) {
15744+
const nameType = getNameTypeFromMappedType(objectType);
15745+
if (!nameType || isTypeAssignableTo(nameType, getTypeParameterFromMappedType(objectType))) {
15746+
return type[cache] = mapType(substituteIndexedMappedType(objectType, type.indexType), t => getSimplifiedType(t, writing));
15747+
}
1574515748
}
1574615749
return type[cache] = type;
1574715750
}

0 commit comments

Comments
 (0)