You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
String enum can't be used to index into an object #16760
enumName{A="a",B="b",C="c",}// interfaces with computed property names are not supported yet, see https://github.com/Microsoft/TypeScript/issues/5579interfaceItem{/* [Name.A] */a: string;/* [Name.B] */b: number;/* [Name.C] */c: boolean;}constnames: Name[]=[Name.A,Name.B,Name.C];constitem: Item={[Name.A]: "a",[Name.B]: 1,[Name.C]: true,};names.forEach((name: Name)=>{console.log(item[name]);});
Expected behavior:
No errors are thrown (ideally also when using the commented code with computed interface property names).
Actual behavior:
src/test.ts(16,7): error TS2322: Type '{ [x: string]: string | number | boolean; }' is not assignable to type 'Item'.
Property 'a' is missing in type '{ [x: string]: string | number | boolean; }'.
src/test.ts(23,17): error TS7017: Element implicitly has an 'any' type because type 'Item' has no index signature.
I'm not sure if there's a way around the second one other than asserting as keyof typeof item. However, it should work if Name is used for a mapped type definition, or when it's used in a computed key once #5579 works.
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).
I would say the change should be as simple as adding a clause to the if statement in getIndexedAccessType.
Reacted by Timofey Kachalov, Aluan Haddad, Janeene Beeforth and Brenton Simpson
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:
Activity
zakjan commentedon Jun 27, 2017
The first error is duplicate of #16687, the second error is new.
Jessidhia commentedon Jun 28, 2017
I'm not sure if there's a way around the second one other than asserting
as keyof typeof item. However, it should work ifNameis used for a mapped type definition, or when it's used in a computed key once #5579 works.ciekawy commentedon Aug 18, 2017
just to jump in with related question, what about
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
Namemhegazy commentedon Aug 18, 2017
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).
I would say the change should be as simple as adding a clause to the if statement in
getIndexedAccessType.[-]Errors when using string enums as property names[/-][+]String enum can't be used to index into an object[/+]sandersn commentedon Aug 24, 2017
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:
sandersn commentedon Aug 24, 2017
Fix is up at #18029