-
Notifications
You must be signed in to change notification settings - Fork 12.9k
check index access for fixed length tuple #26292
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/baselines/reference/bestCommonTypeOfTuple.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(22,13): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(23,13): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(24,13): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(25,13): error TS2733: Index '3' is out-of-bounds in tuple of length 3. | ||
|
||
|
||
==== tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts (4 errors) ==== | ||
function f1(x: number): string { return "foo"; } | ||
|
||
function f2(x: number): number { return 10; } | ||
|
||
function f3(x: number): boolean { return true; } | ||
|
||
enum E1 { one } | ||
|
||
enum E2 { two } | ||
|
||
|
||
var t1: [(x: number) => string, (x: number) => number]; | ||
var t2: [E1, E2]; | ||
var t3: [number, any]; | ||
var t4: [E1, E2, number]; | ||
|
||
// no error | ||
t1 = [f1, f2]; | ||
t2 = [E1.one, E2.two]; | ||
t3 = [5, undefined]; | ||
t4 = [E1.one, E2.two, 20]; | ||
var e1 = t1[2]; // {} | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var e2 = t2[2]; // {} | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var e3 = t3[2]; // any | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var e4 = t4[3]; // number | ||
~ | ||
!!! error TS2733: Index '3' is out-of-bounds in tuple of length 3. |
40 changes: 40 additions & 0 deletions
40
tests/baselines/reference/bestCommonTypeOfTuple2.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(17,14): error TS2733: Index '4' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(18,14): error TS2733: Index '4' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(19,14): error TS2733: Index '4' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(20,14): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(21,14): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
|
||
|
||
==== tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts (5 errors) ==== | ||
interface base { } | ||
interface base1 { i } | ||
class C implements base { c } | ||
class D implements base { d } | ||
class E implements base { e } | ||
class F extends C { f } | ||
|
||
class C1 implements base1 { i = "foo"; c } | ||
class D1 extends C1 { i = "bar"; d } | ||
|
||
var t1: [C, base]; | ||
var t2: [C, D]; | ||
var t3: [C1, D1]; | ||
var t4: [base1, C1]; | ||
var t5: [C1, F] | ||
|
||
var e11 = t1[4]; // base | ||
~ | ||
!!! error TS2733: Index '4' is out-of-bounds in tuple of length 2. | ||
var e21 = t2[4]; // {} | ||
~ | ||
!!! error TS2733: Index '4' is out-of-bounds in tuple of length 2. | ||
var e31 = t3[4]; // C1 | ||
~ | ||
!!! error TS2733: Index '4' is out-of-bounds in tuple of length 2. | ||
var e41 = t4[2]; // base1 | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var e51 = t5[2]; // {} | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/baselines/reference/emptyTuplesTypeAssertion01.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts(2,11): error TS2733: Index '0' is out-of-bounds in tuple of length 0. | ||
|
||
|
||
==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts (1 errors) ==== | ||
let x = <[]>[]; | ||
let y = x[0]; | ||
~ | ||
!!! error TS2733: Index '0' is out-of-bounds in tuple of length 0. |
8 changes: 8 additions & 0 deletions
8
tests/baselines/reference/emptyTuplesTypeAssertion02.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts(2,11): error TS2733: Index '0' is out-of-bounds in tuple of length 0. | ||
|
||
|
||
==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts (1 errors) ==== | ||
let x = [] as []; | ||
let y = x[0]; | ||
~ | ||
!!! error TS2733: Index '0' is out-of-bounds in tuple of length 0. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
tests/cases/conformance/types/tuple/indexerWithTuple.ts(11,25): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/tuple/indexerWithTuple.ts(17,27): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/tuple/indexerWithTuple.ts(20,30): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/tuple/indexerWithTuple.ts(28,30): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
|
||
|
||
==== tests/cases/conformance/types/tuple/indexerWithTuple.ts (4 errors) ==== | ||
var strNumTuple: [string, number] = ["foo", 10]; | ||
var numTupleTuple: [number, [string, number]] = [10, ["bar", 20]]; | ||
var unionTuple1: [number, string| number] = [10, "foo"]; | ||
var unionTuple2: [boolean, string| number] = [true, "foo"]; | ||
|
||
// no error | ||
var idx0 = 0; | ||
var idx1 = 1; | ||
var ele10 = strNumTuple[0]; // string | ||
var ele11 = strNumTuple[1]; // number | ||
var ele12 = strNumTuple[2]; // string | number | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var ele13 = strNumTuple[idx0]; // string | number | ||
var ele14 = strNumTuple[idx1]; // string | number | ||
var ele15 = strNumTuple["0"]; // string | ||
var ele16 = strNumTuple["1"]; // number | ||
var strNumTuple1 = numTupleTuple[1]; //[string, number]; | ||
var ele17 = numTupleTuple[2]; // number | [string, number] | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var eleUnion10 = unionTuple1[0]; // number | ||
var eleUnion11 = unionTuple1[1]; // string | number | ||
var eleUnion12 = unionTuple1[2]; // string | number | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var eleUnion13 = unionTuple1[idx0]; // string | number | ||
var eleUnion14 = unionTuple1[idx1]; // string | number | ||
var eleUnion15 = unionTuple1["0"]; // number | ||
var eleUnion16 = unionTuple1["1"]; // string | number | ||
|
||
var eleUnion20 = unionTuple2[0]; // boolean | ||
var eleUnion21 = unionTuple2[1]; // string | number | ||
var eleUnion22 = unionTuple2[2]; // string | number | boolean | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
var eleUnion23 = unionTuple2[idx0]; // string | number | boolean | ||
var eleUnion24 = unionTuple2[idx1]; // string | number | boolean | ||
var eleUnion25 = unionTuple2["0"]; // boolean | ||
var eleUnion26 = unionTuple2["1"]; // string | number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
tests/cases/conformance/types/tuple/tupleLengthCheck.ts(5,14): error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
tests/cases/conformance/types/tuple/tupleLengthCheck.ts(6,14): error TS2733: Index '1000' is out-of-bounds in tuple of length 2. | ||
|
||
|
||
==== tests/cases/conformance/types/tuple/tupleLengthCheck.ts (2 errors) ==== | ||
declare const a: [number, string] | ||
declare const rest: [number, string, ...boolean[]] | ||
|
||
const a1 = a[1] | ||
const a2 = a[2] | ||
~ | ||
!!! error TS2733: Index '2' is out-of-bounds in tuple of length 2. | ||
const a3 = a[1000] | ||
~~~~ | ||
!!! error TS2733: Index '1000' is out-of-bounds in tuple of length 2. | ||
|
||
const a4 = rest[1] | ||
const a5 = rest[2] | ||
const a6 = rest[3] | ||
const a7 = rest[1000] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//// [tupleLengthCheck.ts] | ||
declare const a: [number, string] | ||
declare const rest: [number, string, ...boolean[]] | ||
|
||
const a1 = a[1] | ||
const a2 = a[2] | ||
const a3 = a[1000] | ||
|
||
const a4 = rest[1] | ||
const a5 = rest[2] | ||
const a6 = rest[3] | ||
const a7 = rest[1000] | ||
|
||
|
||
//// [tupleLengthCheck.js] | ||
var a1 = a[1]; | ||
var a2 = a[2]; | ||
var a3 = a[1000]; | ||
var a4 = rest[1]; | ||
var a5 = rest[2]; | ||
var a6 = rest[3]; | ||
var a7 = rest[1000]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
=== tests/cases/conformance/types/tuple/tupleLengthCheck.ts === | ||
declare const a: [number, string] | ||
>a : Symbol(a, Decl(tupleLengthCheck.ts, 0, 13)) | ||
|
||
declare const rest: [number, string, ...boolean[]] | ||
>rest : Symbol(rest, Decl(tupleLengthCheck.ts, 1, 13)) | ||
|
||
const a1 = a[1] | ||
>a1 : Symbol(a1, Decl(tupleLengthCheck.ts, 3, 5)) | ||
>a : Symbol(a, Decl(tupleLengthCheck.ts, 0, 13)) | ||
>1 : Symbol(1) | ||
|
||
const a2 = a[2] | ||
>a2 : Symbol(a2, Decl(tupleLengthCheck.ts, 4, 5)) | ||
>a : Symbol(a, Decl(tupleLengthCheck.ts, 0, 13)) | ||
|
||
const a3 = a[1000] | ||
>a3 : Symbol(a3, Decl(tupleLengthCheck.ts, 5, 5)) | ||
>a : Symbol(a, Decl(tupleLengthCheck.ts, 0, 13)) | ||
|
||
const a4 = rest[1] | ||
>a4 : Symbol(a4, Decl(tupleLengthCheck.ts, 7, 5)) | ||
>rest : Symbol(rest, Decl(tupleLengthCheck.ts, 1, 13)) | ||
>1 : Symbol(1) | ||
|
||
const a5 = rest[2] | ||
>a5 : Symbol(a5, Decl(tupleLengthCheck.ts, 8, 5)) | ||
>rest : Symbol(rest, Decl(tupleLengthCheck.ts, 1, 13)) | ||
|
||
const a6 = rest[3] | ||
>a6 : Symbol(a6, Decl(tupleLengthCheck.ts, 9, 5)) | ||
>rest : Symbol(rest, Decl(tupleLengthCheck.ts, 1, 13)) | ||
|
||
const a7 = rest[1000] | ||
>a7 : Symbol(a7, Decl(tupleLengthCheck.ts, 10, 5)) | ||
>rest : Symbol(rest, Decl(tupleLengthCheck.ts, 1, 13)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
=== tests/cases/conformance/types/tuple/tupleLengthCheck.ts === | ||
declare const a: [number, string] | ||
>a : [number, string] | ||
|
||
declare const rest: [number, string, ...boolean[]] | ||
>rest : [number, string, ...boolean[]] | ||
|
||
const a1 = a[1] | ||
>a1 : string | ||
>a[1] : string | ||
>a : [number, string] | ||
>1 : 1 | ||
|
||
const a2 = a[2] | ||
>a2 : string | number | ||
>a[2] : string | number | ||
>a : [number, string] | ||
>2 : 2 | ||
|
||
const a3 = a[1000] | ||
>a3 : string | number | ||
>a[1000] : string | number | ||
>a : [number, string] | ||
>1000 : 1000 | ||
|
||
const a4 = rest[1] | ||
>a4 : string | ||
>rest[1] : string | ||
>rest : [number, string, ...boolean[]] | ||
>1 : 1 | ||
|
||
const a5 = rest[2] | ||
>a5 : boolean | ||
>rest[2] : boolean | ||
>rest : [number, string, ...boolean[]] | ||
>2 : 2 | ||
|
||
const a6 = rest[3] | ||
>a6 : boolean | ||
>rest[3] : boolean | ||
>rest : [number, string, ...boolean[]] | ||
>3 : 3 | ||
|
||
const a7 = rest[1000] | ||
>a7 : boolean | ||
>rest[1000] : boolean | ||
>rest : [number, string, ...boolean[]] | ||
>1000 : 1000 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed after message change.
Looks like there should belength(...) - 1
to get last available index (or add- 1
to error param). Otherwise errors looks off by 1 and confusing.P.S. Thank you for PR. Glad to see that this will be in TS soon. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, my fault