@@ -16032,14 +16032,24 @@ namespace ts {
16032
16032
}
16033
16033
16034
16034
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;
16041
16050
}
16042
16051
}
16052
+ error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
16043
16053
return type;
16044
16054
}
16045
16055
0 commit comments