-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Implicit index signatures for enum object types #31687
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
6 commits
Select commit
Hold shift + click to select a range
b688e25
Implement inferrable index signatures for enum types
ahejlsberg a95d18e
Accept new baselines
ahejlsberg 0b53b8e
Merge branch 'master' into enumImplicitIndexSignatures
ahejlsberg cf1bceb
Add tests
ahejlsberg 8bd6fd8
Accept new baselines
ahejlsberg bb15df3
Fix lint error
ahejlsberg 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
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
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.
Why is this
unknown
and not an error? (Is it just because inference fails and "a thing with an inferrable index" is assignable to a{[x: number]: unknown}
?) I'd think that yeah,E2
has an inferrable index type, sure, but we know that it has no numbers in it at all. But, from the stance that the number index signature (were it present in the type) would need to be assignable to the string index, this'd need to beE2
.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.
Because we infer nothing for
T
in the call togetNumberIndexValue
. Keep in mind we're not doing an assignment here, we're just doing inference.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.
Well, we are doing an assignment - we're saying that a
{[x: string]: E2}
is assignable to a{[x: number]: unknown}
. I guess the output type here just irks me a tad - it could only possibly be anE2
orkeyof typeof E2
, rather than anunknown
.Like, if I write
enum StrNum { Zero = "0", One = "1" }
, indexing by0
or1
is gunna get meZero
orOne
, likewise if I writeenum NumStr { "0" = "Zero", "1" = "One" }
, indexing byZero
orOne
is gunna get me"0"
or"1"
.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, indexing your
StrNum
by0
or1
is going to get youundefined
(there's no reverse mapping for string valued members), andNumStr
is an error because members can't have numeric names.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.
Anyway, the
unknown
type is consistent with what we infer for object literal types that have no numerically named members. In some sense it would be more consistent to returnundefined
since that's what you're going to get, but that really is an orthogonal issue.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 always forget about that.
Oh, ick. Ok.