Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loosen number index check, fixes #15768 #17767

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18758,13 +18758,10 @@ namespace ts {
if (isTypeAssignableTo(indexType, getIndexType(objectType))) {
return type;
}
// Check if we're indexing with a numeric type and the object type is a generic
// type with a constraint that has a numeric index signature.
if (maybeTypeOfKind(objectType, TypeFlags.TypeVariable) && isTypeAssignableToKind(indexType, TypeFlags.NumberLike)) {
const constraint = getBaseConstraintOfType(objectType);
if (constraint && getIndexInfoOfType(constraint, IndexKind.Number)) {
return type;
}
// Check if we're indexing with a numeric type and if either object or index types
// is a generic type with a constraint that has a numeric index signature.
if (getIndexInfoOfType(getApparentType(objectType), IndexKind.Number) && isTypeAssignableToKind(indexType, TypeFlags.NumberLike)) {
return type;
}
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
return type;
Expand Down
5 changes: 5 additions & 0 deletions tests/baselines/reference/genericNumberIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//// [genericNumberIndex.ts]
type X<I extends number> = ['a'][I];


//// [genericNumberIndex.js]
6 changes: 6 additions & 0 deletions tests/baselines/reference/genericNumberIndex.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/genericNumberIndex.ts ===
type X<I extends number> = ['a'][I];
>X : Symbol(X, Decl(genericNumberIndex.ts, 0, 0))
>I : Symbol(I, Decl(genericNumberIndex.ts, 0, 7))
>I : Symbol(I, Decl(genericNumberIndex.ts, 0, 7))

6 changes: 6 additions & 0 deletions tests/baselines/reference/genericNumberIndex.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/genericNumberIndex.ts ===
type X<I extends number> = ['a'][I];
>X : ["a"][I]
>I : I
>I : I

1 change: 1 addition & 0 deletions tests/cases/compiler/genericNumberIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type X<I extends number> = ['a'][I];