Skip to content

Commit 919e682

Browse files
committed
Allow T[N] where N is numeric and T has apparent numeric index signature
1 parent 3b0f2c1 commit 919e682

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16032,14 +16032,24 @@ namespace ts {
1603216032
}
1603316033

1603416034
function checkIndexedAccessIndexType(type: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode) {
16035-
if (type.flags & TypeFlags.IndexedAccess) {
16036-
// Check that the index type is assignable to 'keyof T' for the object type.
16037-
const objectType = (<IndexedAccessType>type).objectType;
16038-
const indexType = (<IndexedAccessType>type).indexType;
16039-
if (!isTypeAssignableTo(indexType, getIndexType(objectType))) {
16040-
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
16035+
if (!(type.flags & TypeFlags.IndexedAccess)) {
16036+
return type;
16037+
}
16038+
// Check if the index type is assignable to 'keyof T' for the object type.
16039+
const objectType = (<IndexedAccessType>type).objectType;
16040+
const indexType = (<IndexedAccessType>type).indexType;
16041+
if (isTypeAssignableTo(indexType, getIndexType(objectType))) {
16042+
return type;
16043+
}
16044+
// Check if we're indexing with a numeric type and the object type is a generic
16045+
// type with a constraint that has a numeric index signature.
16046+
if (maybeTypeOfKind(objectType, TypeFlags.TypeVariable) && isTypeOfKind(indexType, TypeFlags.NumberLike)) {
16047+
const constraint = getBaseConstraintOfType(<TypeVariable | UnionOrIntersectionType>objectType);
16048+
if (constraint && getIndexInfoOfType(constraint, IndexKind.Number)) {
16049+
return type;
1604116050
}
1604216051
}
16052+
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
1604316053
return type;
1604416054
}
1604516055

0 commit comments

Comments
 (0)