Skip to content

Commit 616bb5f

Browse files
committed
Defer mapped type indexed access transformations
1 parent 2fede09 commit 616bb5f

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5906,10 +5906,6 @@ namespace ts {
59065906
}
59075907

59085908
function getConstraintOfIndexedAccess(type: IndexedAccessType) {
5909-
const transformed = getTransformedIndexedAccessType(type);
5910-
if (transformed) {
5911-
return transformed;
5912-
}
59135909
const baseObjectType = getBaseConstraintOfType(type.objectType);
59145910
const baseIndexType = getBaseConstraintOfType(type.indexType);
59155911
return baseObjectType || baseIndexType ? getIndexedAccessType(baseObjectType || type.objectType, baseIndexType || type.indexType) : undefined;
@@ -7596,22 +7592,6 @@ namespace ts {
75967592
return anyType;
75977593
}
75987594

7599-
function getIndexedAccessForMappedType(type: MappedType, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode) {
7600-
if (accessNode) {
7601-
// Check if the index type is assignable to 'keyof T' for the object type.
7602-
if (!isTypeAssignableTo(indexType, getIndexType(type))) {
7603-
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(type));
7604-
return unknownType;
7605-
}
7606-
if (accessNode.kind === SyntaxKind.ElementAccessExpression && isAssignmentTarget(accessNode) && type.declaration.readonlyToken) {
7607-
error(accessNode, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(type));
7608-
}
7609-
}
7610-
const mapper = createTypeMapper([getTypeParameterFromMappedType(type)], [indexType]);
7611-
const templateMapper = type.mapper ? combineTypeMappers(type.mapper, mapper) : mapper;
7612-
return instantiateType(getTemplateTypeFromMappedType(type), templateMapper);
7613-
}
7614-
76157595
function isGenericObjectType(type: Type): boolean {
76167596
return type.flags & TypeFlags.TypeVariable ? true :
76177597
getObjectFlags(type) & ObjectFlags.Mapped ? isGenericIndexType(getConstraintTypeFromMappedType(<MappedType>type)) :
@@ -7637,12 +7617,14 @@ namespace ts {
76377617
return false;
76387618
}
76397619

7640-
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
7641-
// more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a
7642-
// transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed
7643-
// access types with default property values as expressed by D.
7620+
// Transform an indexed access occurring in a read position to a simpler form. Return the simpler form,
7621+
// or undefined if no transformation is possible.
76447622
function getTransformedIndexedAccessType(type: IndexedAccessType): Type {
76457623
const objectType = type.objectType;
7624+
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
7625+
// more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a
7626+
// transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed
7627+
// access types with default property values as expressed by D.
76467628
if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType) && some((<IntersectionType>objectType).types, isStringIndexOnlyType)) {
76477629
const regularTypes: Type[] = [];
76487630
const stringIndexTypes: Type[] = [];
@@ -7659,20 +7641,23 @@ namespace ts {
76597641
getIntersectionType(stringIndexTypes)
76607642
]);
76617643
}
7662-
return undefined;
7663-
}
7664-
7665-
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode): Type {
7666-
// If the object type is a mapped type { [P in K]: E }, where K is generic, we instantiate E using a mapper
7644+
// If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper
76677645
// that substitutes the index type for P. For example, for an index access { [P in K]: Box<T[P]> }[X], we
76687646
// construct the type Box<T[X]>.
76697647
if (isGenericMappedType(objectType)) {
7670-
return getIndexedAccessForMappedType(<MappedType>objectType, indexType, accessNode);
7648+
const mapper = createTypeMapper([getTypeParameterFromMappedType(<MappedType>objectType)], [type.indexType]);
7649+
const objectTypeMapper = (<MappedType>objectType).mapper;
7650+
const templateMapper = objectTypeMapper ? combineTypeMappers(objectTypeMapper, mapper) : mapper;
7651+
return instantiateType(getTemplateTypeFromMappedType(<MappedType>objectType), templateMapper);
76717652
}
7672-
// Otherwise, if the index type is generic, or if the object type is generic and doesn't originate in an
7673-
// expression, we are performing a higher-order index access where we cannot meaningfully access the properties
7674-
// of the object type. Note that for a generic T and a non-generic K, we eagerly resolve T[K] if it originates
7675-
// in an expression. This is to preserve backwards compatibility. For example, an element access 'this["foo"]'
7653+
return undefined;
7654+
}
7655+
7656+
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode): Type {
7657+
// If the index type is generic, or if the object type is generic and doesn't originate in an expression,
7658+
// we are performing a higher-order index access where we cannot meaningfully access the properties of the
7659+
// object type. Note that for a generic T and a non-generic K, we eagerly resolve T[K] if it originates in
7660+
// an expression. This is to preserve backwards compatibility. For example, an element access 'this["foo"]'
76767661
// has always been resolved eagerly using the constraint type of 'this' at the given location.
76777662
if (isGenericIndexType(indexType) || !(accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression) && isGenericObjectType(objectType)) {
76787663
if (objectType.flags & TypeFlags.Any) {
@@ -9334,7 +9319,7 @@ namespace ts {
93349319
else if (source.flags & TypeFlags.IndexedAccess) {
93359320
// A type S[K] is related to a type T if A[K] is related to T, where K is string-like and
93369321
// A is the apparent type of S.
9337-
const constraint = getConstraintOfType(<IndexedAccessType>source);
9322+
const constraint = getTransformedIndexedAccessType(<IndexedAccessType>source) || getConstraintOfIndexedAccess(<IndexedAccessType>source);
93389323
if (constraint) {
93399324
if (result = isRelatedTo(constraint, target, reportErrors)) {
93409325
errorInfo = saveErrorInfo;
@@ -18810,6 +18795,10 @@ namespace ts {
1881018795
const objectType = (<IndexedAccessType>type).objectType;
1881118796
const indexType = (<IndexedAccessType>type).indexType;
1881218797
if (isTypeAssignableTo(indexType, getIndexType(objectType))) {
18798+
if (accessNode.kind === SyntaxKind.ElementAccessExpression && isAssignmentTarget(accessNode) &&
18799+
getObjectFlags(objectType) & ObjectFlags.Mapped && (<MappedType>objectType).declaration.readonlyToken) {
18800+
error(accessNode, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
18801+
}
1881318802
return type;
1881418803
}
1881518804
// Check if we're indexing with a numeric type and if either object or index types

0 commit comments

Comments
 (0)