Skip to content

Commit 4ac8976

Browse files
authored
Always check index type validity for all types when an error node is present so we always issue an error (#26789)
* Always check index type validity for all types when an error node is present so we always issue an error * Change type a bit
1 parent ca66241 commit 4ac8976

7 files changed

+68
-2
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9380,13 +9380,24 @@ namespace ts {
93809380
const apparentObjectType = getApparentType(objectType);
93819381
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) {
93829382
const propTypes: Type[] = [];
9383+
let wasMissingProp = false;
93839384
for (const t of (<UnionType>indexType).types) {
93849385
const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false, missingType);
93859386
if (propType === missingType) {
9386-
return missingType;
9387+
if (!accessNode) {
9388+
// If there's no error node, we can immeditely stop, since error reporting is off
9389+
return missingType;
9390+
}
9391+
else {
9392+
// Otherwise we set a flag and return at the end of the loop so we still mark all errors
9393+
wasMissingProp = true;
9394+
}
93879395
}
93889396
propTypes.push(propType);
93899397
}
9398+
if (wasMissingProp) {
9399+
return missingType;
9400+
}
93909401
return getUnionType(propTypes);
93919402
}
93929403
return getPropertyTypeForIndexType(apparentObjectType, indexType, accessNode, /*cacheSymbol*/ true, missingType);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/arrayIndexWithArrayFails.ts(3,16): error TS2538: Type 'string[]' cannot be used as an index type.
2+
3+
4+
==== tests/cases/compiler/arrayIndexWithArrayFails.ts (1 errors) ====
5+
declare const arr1: (string | string[])[];
6+
declare const arr2: number[];
7+
const j = arr2[arr1[0]]; // should error
8+
~~~~~~~
9+
!!! error TS2538: Type 'string[]' cannot be used as an index type.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [arrayIndexWithArrayFails.ts]
2+
declare const arr1: (string | string[])[];
3+
declare const arr2: number[];
4+
const j = arr2[arr1[0]]; // should error
5+
6+
//// [arrayIndexWithArrayFails.js]
7+
var j = arr2[arr1[0]]; // should error
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/arrayIndexWithArrayFails.ts ===
2+
declare const arr1: (string | string[])[];
3+
>arr1 : Symbol(arr1, Decl(arrayIndexWithArrayFails.ts, 0, 13))
4+
5+
declare const arr2: number[];
6+
>arr2 : Symbol(arr2, Decl(arrayIndexWithArrayFails.ts, 1, 13))
7+
8+
const j = arr2[arr1[0]]; // should error
9+
>j : Symbol(j, Decl(arrayIndexWithArrayFails.ts, 2, 5))
10+
>arr2 : Symbol(arr2, Decl(arrayIndexWithArrayFails.ts, 1, 13))
11+
>arr1 : Symbol(arr1, Decl(arrayIndexWithArrayFails.ts, 0, 13))
12+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/arrayIndexWithArrayFails.ts ===
2+
declare const arr1: (string | string[])[];
3+
>arr1 : (string | string[])[]
4+
5+
declare const arr2: number[];
6+
>arr2 : number[]
7+
8+
const j = arr2[arr1[0]]; // should error
9+
>j : any
10+
>arr2[arr1[0]] : any
11+
>arr2 : number[]
12+
>arr1[0] : string | string[]
13+
>arr1 : (string | string[])[]
14+
>0 : 0
15+

tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(25,18): error
88
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(26,18): error TS2538: Type 'void' cannot be used as an index type.
99
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(27,18): error TS2538: Type 'undefined' cannot be used as an index type.
1010
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(28,18): error TS2538: Type '{ x: string; }' cannot be used as an index type.
11+
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(29,18): error TS2537: Type 'Shape' has no matching index signature for type 'number'.
1112
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(29,18): error TS2537: Type 'Shape' has no matching index signature for type 'string'.
1213
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(30,18): error TS2538: Type 'string & number' cannot be used as an index type.
1314
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(31,18): error TS2537: Type 'Shape' has no matching index signature for type 'string'.
15+
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(31,18): error TS2538: Type 'false' cannot be used as an index type.
16+
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(31,18): error TS2538: Type 'true' cannot be used as an index type.
1417
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(35,21): error TS2537: Type 'string[]' has no matching index signature for type 'string'.
1518
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(36,21): error TS2538: Type 'boolean' cannot be used as an index type.
1619
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(41,31): error TS2538: Type 'boolean' cannot be used as an index type.
@@ -60,7 +63,7 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error
6063
Type 'T' is not assignable to type 'U'.
6164

6265

63-
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (33 errors) ====
66+
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (36 errors) ====
6467
class Shape {
6568
name: string;
6669
width: number;
@@ -111,13 +114,19 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error
111114
!!! error TS2538: Type '{ x: string; }' cannot be used as an index type.
112115
type T20 = Shape[string | number]; // Error
113116
~~~~~~~~~~~~~~~
117+
!!! error TS2537: Type 'Shape' has no matching index signature for type 'number'.
118+
~~~~~~~~~~~~~~~
114119
!!! error TS2537: Type 'Shape' has no matching index signature for type 'string'.
115120
type T21 = Shape[string & number]; // Error
116121
~~~~~~~~~~~~~~~
117122
!!! error TS2538: Type 'string & number' cannot be used as an index type.
118123
type T22 = Shape[string | boolean]; // Error
119124
~~~~~~~~~~~~~~~~
120125
!!! error TS2537: Type 'Shape' has no matching index signature for type 'string'.
126+
~~~~~~~~~~~~~~~~
127+
!!! error TS2538: Type 'false' cannot be used as an index type.
128+
~~~~~~~~~~~~~~~~
129+
!!! error TS2538: Type 'true' cannot be used as an index type.
121130

122131
type T30 = string[]["length"];
123132
type T31 = string[][number];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const arr1: (string | string[])[];
2+
declare const arr2: number[];
3+
const j = arr2[arr1[0]]; // should error

0 commit comments

Comments
 (0)