-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Const enum indexed access #5167
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
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//// [constIndexedAccess.ts] | ||
|
||
const enum numbers { | ||
zero, | ||
one | ||
} | ||
|
||
interface indexAccess { | ||
0: string; | ||
1: number; | ||
} | ||
|
||
let test: indexAccess; | ||
|
||
let s = test[0]; | ||
let n = test[1]; | ||
|
||
let s1 = test[numbers.zero]; | ||
let n1 = test[numbers.one]; | ||
|
||
let s2 = test[numbers["zero"]]; | ||
let n2 = test[numbers["one"]]; | ||
|
||
enum numbersNotConst { | ||
zero, | ||
one | ||
} | ||
|
||
let s3 = test[numbersNotConst.zero]; | ||
let n3 = test[numbersNotConst.one]; | ||
|
||
|
||
//// [constIndexedAccess.js] | ||
var test; | ||
var s = test[0]; | ||
var n = test[1]; | ||
var s1 = test[0 /* zero */]; | ||
var n1 = test[1 /* one */]; | ||
var s2 = test[0 /* "zero" */]; | ||
var n2 = test[1 /* "one" */]; | ||
var numbersNotConst; | ||
(function (numbersNotConst) { | ||
numbersNotConst[numbersNotConst["zero"] = 0] = "zero"; | ||
numbersNotConst[numbersNotConst["one"] = 1] = "one"; | ||
})(numbersNotConst || (numbersNotConst = {})); | ||
var s3 = test[numbersNotConst.zero]; | ||
var n3 = test[numbersNotConst.one]; |
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,83 @@ | ||
=== tests/cases/compiler/constIndexedAccess.ts === | ||
|
||
const enum numbers { | ||
>numbers : Symbol(numbers, Decl(constIndexedAccess.ts, 0, 0)) | ||
|
||
zero, | ||
>zero : Symbol(numbers.zero, Decl(constIndexedAccess.ts, 1, 20)) | ||
|
||
one | ||
>one : Symbol(numbers.one, Decl(constIndexedAccess.ts, 2, 9)) | ||
} | ||
|
||
interface indexAccess { | ||
>indexAccess : Symbol(indexAccess, Decl(constIndexedAccess.ts, 4, 1)) | ||
|
||
0: string; | ||
1: number; | ||
} | ||
|
||
let test: indexAccess; | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>indexAccess : Symbol(indexAccess, Decl(constIndexedAccess.ts, 4, 1)) | ||
|
||
let s = test[0]; | ||
>s : Symbol(s, Decl(constIndexedAccess.ts, 13, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>0 : Symbol(indexAccess.0, Decl(constIndexedAccess.ts, 6, 23)) | ||
|
||
let n = test[1]; | ||
>n : Symbol(n, Decl(constIndexedAccess.ts, 14, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>1 : Symbol(indexAccess.1, Decl(constIndexedAccess.ts, 7, 14)) | ||
|
||
let s1 = test[numbers.zero]; | ||
>s1 : Symbol(s1, Decl(constIndexedAccess.ts, 16, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>numbers.zero : Symbol(numbers.zero, Decl(constIndexedAccess.ts, 1, 20)) | ||
>numbers : Symbol(numbers, Decl(constIndexedAccess.ts, 0, 0)) | ||
>zero : Symbol(numbers.zero, Decl(constIndexedAccess.ts, 1, 20)) | ||
|
||
let n1 = test[numbers.one]; | ||
>n1 : Symbol(n1, Decl(constIndexedAccess.ts, 17, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>numbers.one : Symbol(numbers.one, Decl(constIndexedAccess.ts, 2, 9)) | ||
>numbers : Symbol(numbers, Decl(constIndexedAccess.ts, 0, 0)) | ||
>one : Symbol(numbers.one, Decl(constIndexedAccess.ts, 2, 9)) | ||
|
||
let s2 = test[numbers["zero"]]; | ||
>s2 : Symbol(s2, Decl(constIndexedAccess.ts, 19, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>numbers : Symbol(numbers, Decl(constIndexedAccess.ts, 0, 0)) | ||
>"zero" : Symbol(numbers.zero, Decl(constIndexedAccess.ts, 1, 20)) | ||
|
||
let n2 = test[numbers["one"]]; | ||
>n2 : Symbol(n2, Decl(constIndexedAccess.ts, 20, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>numbers : Symbol(numbers, Decl(constIndexedAccess.ts, 0, 0)) | ||
>"one" : Symbol(numbers.one, Decl(constIndexedAccess.ts, 2, 9)) | ||
|
||
enum numbersNotConst { | ||
>numbersNotConst : Symbol(numbersNotConst, Decl(constIndexedAccess.ts, 20, 30)) | ||
|
||
zero, | ||
>zero : Symbol(numbersNotConst.zero, Decl(constIndexedAccess.ts, 22, 22)) | ||
|
||
one | ||
>one : Symbol(numbersNotConst.one, Decl(constIndexedAccess.ts, 23, 9)) | ||
} | ||
|
||
let s3 = test[numbersNotConst.zero]; | ||
>s3 : Symbol(s3, Decl(constIndexedAccess.ts, 27, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>numbersNotConst.zero : Symbol(numbersNotConst.zero, Decl(constIndexedAccess.ts, 22, 22)) | ||
>numbersNotConst : Symbol(numbersNotConst, Decl(constIndexedAccess.ts, 20, 30)) | ||
>zero : Symbol(numbersNotConst.zero, Decl(constIndexedAccess.ts, 22, 22)) | ||
|
||
let n3 = test[numbersNotConst.one]; | ||
>n3 : Symbol(n3, Decl(constIndexedAccess.ts, 28, 3)) | ||
>test : Symbol(test, Decl(constIndexedAccess.ts, 11, 3)) | ||
>numbersNotConst.one : Symbol(numbersNotConst.one, Decl(constIndexedAccess.ts, 23, 9)) | ||
>numbersNotConst : Symbol(numbersNotConst, Decl(constIndexedAccess.ts, 20, 30)) | ||
>one : Symbol(numbersNotConst.one, Decl(constIndexedAccess.ts, 23, 9)) | ||
|
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,93 @@ | ||
=== tests/cases/compiler/constIndexedAccess.ts === | ||
|
||
const enum numbers { | ||
>numbers : numbers | ||
|
||
zero, | ||
>zero : numbers | ||
|
||
one | ||
>one : numbers | ||
} | ||
|
||
interface indexAccess { | ||
>indexAccess : indexAccess | ||
|
||
0: string; | ||
1: number; | ||
} | ||
|
||
let test: indexAccess; | ||
>test : indexAccess | ||
>indexAccess : indexAccess | ||
|
||
let s = test[0]; | ||
>s : string | ||
>test[0] : string | ||
>test : indexAccess | ||
>0 : number | ||
|
||
let n = test[1]; | ||
>n : number | ||
>test[1] : number | ||
>test : indexAccess | ||
>1 : number | ||
|
||
let s1 = test[numbers.zero]; | ||
>s1 : string | ||
>test[numbers.zero] : string | ||
>test : indexAccess | ||
>numbers.zero : numbers | ||
>numbers : typeof numbers | ||
>zero : numbers | ||
|
||
let n1 = test[numbers.one]; | ||
>n1 : number | ||
>test[numbers.one] : number | ||
>test : indexAccess | ||
>numbers.one : numbers | ||
>numbers : typeof numbers | ||
>one : numbers | ||
|
||
let s2 = test[numbers["zero"]]; | ||
>s2 : string | ||
>test[numbers["zero"]] : string | ||
>test : indexAccess | ||
>numbers["zero"] : numbers | ||
>numbers : typeof numbers | ||
>"zero" : string | ||
|
||
let n2 = test[numbers["one"]]; | ||
>n2 : number | ||
>test[numbers["one"]] : number | ||
>test : indexAccess | ||
>numbers["one"] : numbers | ||
>numbers : typeof numbers | ||
>"one" : string | ||
|
||
enum numbersNotConst { | ||
>numbersNotConst : numbersNotConst | ||
|
||
zero, | ||
>zero : numbersNotConst | ||
|
||
one | ||
>one : numbersNotConst | ||
} | ||
|
||
let s3 = test[numbersNotConst.zero]; | ||
>s3 : any | ||
>test[numbersNotConst.zero] : any | ||
>test : indexAccess | ||
>numbersNotConst.zero : numbersNotConst | ||
>numbersNotConst : typeof numbersNotConst | ||
>zero : numbersNotConst | ||
|
||
let n3 = test[numbersNotConst.one]; | ||
>n3 : any | ||
>test[numbersNotConst.one] : any | ||
>test : indexAccess | ||
>numbersNotConst.one : numbersNotConst | ||
>numbersNotConst : typeof numbersNotConst | ||
>one : numbersNotConst | ||
|
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 @@ | ||
| ||
const enum numbers { | ||
zero, | ||
one | ||
} | ||
|
||
interface indexAccess { | ||
0: string; | ||
1: number; | ||
} | ||
|
||
let test: indexAccess; | ||
|
||
let s = test[0]; | ||
let n = test[1]; | ||
|
||
let s1 = test[numbers.zero]; | ||
let n1 = test[numbers.one]; | ||
|
||
let s2 = test[numbers["zero"]]; | ||
let n2 = test[numbers["one"]]; | ||
|
||
enum numbersNotConst { | ||
zero, | ||
one | ||
} | ||
|
||
let s3 = test[numbersNotConst.zero]; | ||
let n3 = test[numbersNotConst.one]; |
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.
can you add a test for the indexed access as well.
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.
What do you mean, a test for the non constant enum?
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.
I believe @mhegazy means
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.
Added + the regular enum where you still get
any
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.
yes. sorry about that.