Skip to content

Always check index type validity for all types when an error node is present so we always issue an error #26789

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

Merged
merged 3 commits into from
Sep 4, 2018
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
13 changes: 12 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9356,13 +9356,24 @@ namespace ts {
const apparentObjectType = getApparentType(objectType);
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) {
const propTypes: Type[] = [];
let wasMissingProp = false;
for (const t of (<UnionType>indexType).types) {
const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false, missingType);
if (propType === missingType) {
return missingType;
if (!accessNode) {
// If there's no error node, we can immeditely stop, since error reporting is off
return missingType;
}
else {
// Otherwise we set a flag and return at the end of the loop so we still mark all errors
wasMissingProp = true;
}
}
propTypes.push(propType);
}
if (wasMissingProp) {
return missingType;
}
return getUnionType(propTypes);
}
return getPropertyTypeForIndexType(apparentObjectType, indexType, accessNode, /*cacheSymbol*/ true, missingType);
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/arrayIndexWithArrayFails.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/cases/compiler/arrayIndexWithArrayFails.ts(3,16): error TS2538: Type 'string[]' cannot be used as an index type.


==== tests/cases/compiler/arrayIndexWithArrayFails.ts (1 errors) ====
declare const arr1: (string | string[])[];
declare const arr2: number[];
const j = arr2[arr1[0]]; // should error
~~~~~~~
!!! error TS2538: Type 'string[]' cannot be used as an index type.
7 changes: 7 additions & 0 deletions tests/baselines/reference/arrayIndexWithArrayFails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [arrayIndexWithArrayFails.ts]
declare const arr1: (string | string[])[];
declare const arr2: number[];
const j = arr2[arr1[0]]; // should error

//// [arrayIndexWithArrayFails.js]
var j = arr2[arr1[0]]; // should error
12 changes: 12 additions & 0 deletions tests/baselines/reference/arrayIndexWithArrayFails.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/arrayIndexWithArrayFails.ts ===
declare const arr1: (string | string[])[];
>arr1 : Symbol(arr1, Decl(arrayIndexWithArrayFails.ts, 0, 13))

declare const arr2: number[];
>arr2 : Symbol(arr2, Decl(arrayIndexWithArrayFails.ts, 1, 13))

const j = arr2[arr1[0]]; // should error
>j : Symbol(j, Decl(arrayIndexWithArrayFails.ts, 2, 5))
>arr2 : Symbol(arr2, Decl(arrayIndexWithArrayFails.ts, 1, 13))
>arr1 : Symbol(arr1, Decl(arrayIndexWithArrayFails.ts, 0, 13))

15 changes: 15 additions & 0 deletions tests/baselines/reference/arrayIndexWithArrayFails.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/compiler/arrayIndexWithArrayFails.ts ===
declare const arr1: (string | string[])[];
>arr1 : (string | string[])[]

declare const arr2: number[];
>arr2 : number[]

const j = arr2[arr1[0]]; // should error
>j : any
>arr2[arr1[0]] : any
>arr2 : number[]
>arr1[0] : string | string[]
>arr1 : (string | string[])[]
>0 : 0

11 changes: 10 additions & 1 deletion tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(25,18): error
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(26,18): error TS2538: Type 'void' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(27,18): error TS2538: Type 'undefined' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(28,18): error TS2538: Type '{ x: string; }' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(29,18): error TS2537: Type 'Shape' has no matching index signature for type 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(29,18): error TS2537: Type 'Shape' has no matching index signature for type 'string'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(30,18): error TS2538: Type 'string & number' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(31,18): error TS2537: Type 'Shape' has no matching index signature for type 'string'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(31,18): error TS2538: Type 'false' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(31,18): error TS2538: Type 'true' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(35,21): error TS2537: Type 'string[]' has no matching index signature for type 'string'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(36,21): error TS2538: Type 'boolean' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(41,31): error TS2538: Type 'boolean' cannot be used as an index type.
Expand Down Expand Up @@ -60,7 +63,7 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error
Type 'T' is not assignable to type 'U'.


==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (33 errors) ====
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (36 errors) ====
class Shape {
name: string;
width: number;
Expand Down Expand Up @@ -111,13 +114,19 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error
!!! error TS2538: Type '{ x: string; }' cannot be used as an index type.
type T20 = Shape[string | number]; // Error
~~~~~~~~~~~~~~~
!!! error TS2537: Type 'Shape' has no matching index signature for type 'number'.
~~~~~~~~~~~~~~~
!!! error TS2537: Type 'Shape' has no matching index signature for type 'string'.
type T21 = Shape[string & number]; // Error
~~~~~~~~~~~~~~~
!!! error TS2538: Type 'string & number' cannot be used as an index type.
type T22 = Shape[string | boolean]; // Error
~~~~~~~~~~~~~~~~
!!! error TS2537: Type 'Shape' has no matching index signature for type 'string'.
~~~~~~~~~~~~~~~~
!!! error TS2538: Type 'false' cannot be used as an index type.
~~~~~~~~~~~~~~~~
!!! error TS2538: Type 'true' cannot be used as an index type.

type T30 = string[]["length"];
type T31 = string[][number];
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/compiler/arrayIndexWithArrayFails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const arr1: (string | string[])[];
declare const arr2: number[];
const j = arr2[arr1[0]]; // should error