-
Notifications
You must be signed in to change notification settings - Fork 12.8k
String enum can't be used to index into an object #16760
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
The first error is duplicate of #16687, the second error is new. |
I'm not sure if there's a way around the second one other than asserting |
just to jump in with related question, what about enum Name {
A = 'a',
B = 'b'
};
const item: Item = {
[name: Name]: string;
}; gives an error
could it be possible to use the above approach to expect the property names to use only values defined in an enum |
We do handle union types specially in indexing, we should probably do the same for string enums, since they are strictly a union of string literals (and possibly for all union enums). |
Given that #16687 already tracks the first part of this, let's use this bug to track the last part, the use of a string enum to index into an object type that has the same keys. Here's a cut-down repro: // @noImplicitAny: true
enum Name {
A = "a",
B = "b",
C = "c",
}
interface Item {
a: string;
b: number;
c: boolean;
}
declare const item: Item;
declare const name: Name;
let result = item[name]; |
Fix is up at #18029 |
TypeScript Version: 2.4.0
Code
Expected behavior:
No errors are thrown (ideally also when using the commented code with computed interface property names).
Actual behavior:
The text was updated successfully, but these errors were encountered: