-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type checking 'for...of' in ES3/5 #2308
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5b46f5f
Remove error for using 'for...of' in ES3/ES5
JsonFreeman 61cd2a7
Introduce checkElementTypeOfArrayOrString for downlevel for..of type …
JsonFreeman 32aee67
Change a test to be more interesting
JsonFreeman 29cbe9d
Remove unhelpful comment
JsonFreeman 03176d3
Add tests for downlevel for-of type checking
JsonFreeman 7d2d55e
Rebaseline tests that will be affected by #2308
JsonFreeman 1204d31
Merge branch 'master' of https://github.com/Microsoft/TypeScript into…
JsonFreeman 22f80b9
Adjust baselines after merge
JsonFreeman 6691408
Address PR feedback
JsonFreeman 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
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,7 @@ | ||
tests/cases/conformance/statements/for-ofStatements/ES3For-ofTypeCheck1.ts(1,15): error TS2494: Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-ofStatements/ES3For-ofTypeCheck1.ts (1 errors) ==== | ||
for (var v of "") { } | ||
~~ | ||
!!! error TS2494: Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher. |
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,7 @@ | ||
//// [ES3For-ofTypeCheck1.ts] | ||
for (var v of "") { } | ||
|
||
//// [ES3For-ofTypeCheck1.js] | ||
for (var _i = 0, _a = ""; _i < _a.length; _i++) { | ||
var v = _a[_i]; | ||
} |
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,9 @@ | ||
//// [ES3For-ofTypeCheck2.ts] | ||
for (var v of [true]) { } | ||
|
||
//// [ES3For-ofTypeCheck2.js] | ||
for (var _i = 0, _a = [ | ||
true | ||
]; _i < _a.length; _i++) { | ||
var v = _a[_i]; | ||
} |
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,5 @@ | ||
=== tests/cases/conformance/statements/for-ofStatements/ES3For-ofTypeCheck2.ts === | ||
for (var v of [true]) { } | ||
>v : boolean | ||
>[true] : boolean[] | ||
|
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/statements/for-ofStatements/ES3For-ofTypeCheck4.ts(2,17): error TS2494: Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-ofStatements/ES3For-ofTypeCheck4.ts (1 errors) ==== | ||
var union: string | string[]; | ||
for (const v of union) { } | ||
~~~~~ | ||
!!! error TS2494: Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher. |
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,9 @@ | ||
//// [ES3For-ofTypeCheck4.ts] | ||
var union: string | string[]; | ||
for (const v of union) { } | ||
|
||
//// [ES3For-ofTypeCheck4.js] | ||
var union; | ||
for (var _i = 0; _i < union.length; _i++) { | ||
var v = union[_i]; | ||
} |
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,9 @@ | ||
//// [ES3For-ofTypeCheck6.ts] | ||
var union: string[] | number[]; | ||
for (var v of union) { } | ||
|
||
//// [ES3For-ofTypeCheck6.js] | ||
var union; | ||
for (var _i = 0; _i < union.length; _i++) { | ||
var v = union[_i]; | ||
} |
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/statements/for-ofStatements/ES3For-ofTypeCheck6.ts === | ||
var union: string[] | number[]; | ||
>union : string[] | number[] | ||
|
||
for (var v of union) { } | ||
>v : string | number | ||
>union : 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
tests/cases/conformance/statements/for-ofStatements/ES5For-of1.ts(1,1): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher. | ||
tests/cases/conformance/statements/for-ofStatements/ES5For-of1.ts(2,5): error TS2304: Cannot find name 'console'. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-ofStatements/ES5For-of1.ts (1 errors) ==== | ||
for (var v of ['a', 'b', 'c']) { | ||
~~~ | ||
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher. | ||
console.log(v); | ||
~~~~~~~ | ||
!!! error TS2304: Cannot find name 'console'. | ||
} |
This file was deleted.
Oops, something went wrong.
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,29 @@ | ||
=== tests/cases/conformance/statements/for-ofStatements/ES5For-of10.ts === | ||
function foo() { | ||
>foo : () => { x: number; } | ||
|
||
return { x: 0 }; | ||
>{ x: 0 } : { x: number; } | ||
>x : number | ||
} | ||
for (foo().x of []) { | ||
>foo().x : number | ||
>foo() : { x: number; } | ||
>foo : () => { x: number; } | ||
>x : number | ||
>[] : undefined[] | ||
|
||
for (foo().x of []) | ||
>foo().x : number | ||
>foo() : { x: number; } | ||
>foo : () => { x: number; } | ||
>x : number | ||
>[] : undefined[] | ||
|
||
var p = foo().x; | ||
>p : number | ||
>foo().x : number | ||
>foo() : { x: number; } | ||
>foo : () => { x: number; } | ||
>x : number | ||
} |
This file was deleted.
Oops, something went wrong.
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/statements/for-ofStatements/ES5For-of11.ts === | ||
var v; | ||
>v : any | ||
|
||
for (v of []) { } | ||
>v : any | ||
>[] : undefined[] | ||
|
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.
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.
Could you add, at the top of this function, the types of types you want this fnction to be able to handle, and what final type will be returned?
Note: this function never seems to return 'undefined'. But i thought you used || earlier to default to hte 'any' type of you got 'undefined' back from this.
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.
Yeah I will add a summary of what the result should be, as well as some examples.