Skip to content

Commit a1cbeb2

Browse files
authored
Merge pull request #17767 from tycho01/15768-generic-numeric-index-error
loosen number index check, fixes #15768
2 parents 543e0af + 4268e13 commit a1cbeb2

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18761,13 +18761,10 @@ namespace ts {
1876118761
if (isTypeAssignableTo(indexType, getIndexType(objectType))) {
1876218762
return type;
1876318763
}
18764-
// Check if we're indexing with a numeric type and the object type is a generic
18765-
// type with a constraint that has a numeric index signature.
18766-
if (maybeTypeOfKind(objectType, TypeFlags.TypeVariable) && isTypeAssignableToKind(indexType, TypeFlags.NumberLike)) {
18767-
const constraint = getBaseConstraintOfType(objectType);
18768-
if (constraint && getIndexInfoOfType(constraint, IndexKind.Number)) {
18769-
return type;
18770-
}
18764+
// Check if we're indexing with a numeric type and if either object or index types
18765+
// is a generic type with a constraint that has a numeric index signature.
18766+
if (getIndexInfoOfType(getApparentType(objectType), IndexKind.Number) && isTypeAssignableToKind(indexType, TypeFlags.NumberLike)) {
18767+
return type;
1877118768
}
1877218769
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
1877318770
return type;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [genericNumberIndex.ts]
2+
type X<I extends number> = ['a'][I];
3+
4+
5+
//// [genericNumberIndex.js]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/genericNumberIndex.ts ===
2+
type X<I extends number> = ['a'][I];
3+
>X : Symbol(X, Decl(genericNumberIndex.ts, 0, 0))
4+
>I : Symbol(I, Decl(genericNumberIndex.ts, 0, 7))
5+
>I : Symbol(I, Decl(genericNumberIndex.ts, 0, 7))
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/genericNumberIndex.ts ===
2+
type X<I extends number> = ['a'][I];
3+
>X : ["a"][I]
4+
>I : I
5+
>I : I
6+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type X<I extends number> = ['a'][I];

0 commit comments

Comments
 (0)