Repository navigation
Enums can not be used for index signature types #13042
Description
Activity
- addedIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
on Dec 20, 2016 can we allow
string & Whateverfor index signatures too?Reacted by Chiri Vulpes, Stefano J. Attardi, Paul Koerbitz, mpal9000, grlscz, Aluan Haddad, SlurpTheo, bedorlan, Gabriel Araujo, Addison Rodomista and 4 more- addedCommittedThe team has roadmapped this issueThe team has roadmapped this issueand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Jan 24, 2017 RyanCavanaugh commented
on Jan 24, 2017 MemberMore actionsApproved.
Note that this will not cause nominal enforcement of enum index expressions!
Specifically,
[index in UserInterfaceElement]: ColorInfo;will eagerly reduce to"0" | "1" : ColorInfo, so indexing by any other 0- or 1-valued enum value will not cause an error.Reacted by Chiri Vulpes, Maxim Kulikov, Alexander Field, Sam Maddock, David Hara, SlurpTheo, Tom, Vesa Nikkilä, Yazan Alaboudi, Luciano Lima and 4 moreReacted by Matt HooperThanks for handling this! Waiting for the fix 👍🏻
28 remaining items
ChiriVulpes commented
on Feb 20, 2018 ContributorAuthorMore actionsAnton Fedchenko (@kompot) That's because you're mapping the enums to string values. You're really just using strings as keys. This issue is for the use of numeric enum entries (which would allow auto-assigned enums to work as well)
As far as I know, it's worked via string since
inand string enums were both present in the language.Reacted by Amit BeckensteinWell, it definitely didn't work like 6 months ago when I was banging my head trying to get some concise way of exhaustive enum checking. I understand that this is a different issue, yes. But from my understanding getting it work with auto-assigned enums is not that critical when mapped strings enums work this nice.
- changed the title
[-]Enums can no longer be used for index signature types[/-][+]Enums can not be used for index signature types[/+]on Feb 20, 2018 const enum UnitType { PLAYER = 0x01, MONSTER = 0x02 } const descriptor = ({ [UnitType.PLAYER as number]: Player.descriptor, [UnitType.MONSTER as number]: Monster.descriptor })[type]Doing it like so may help some in the interim
Reacted by Davide Campello, Ajdien Taravati and Frances McNultyReacted by Davide CampelloShould be fixed by #23592
Reacted by Matt, Amit Beckenstein, Jake Lauer, iamjoyce, Zufeng and SergeyReacted by Chiri Vulpes, ZpdDG4gta, Tom Yaxley, Amit Beckenstein, Calle Helmertz, Jake Lauer and SergeyReacted by Amit Beckenstein, Jake Lauer, Yang Lin and SergeyWas this actually fixed by #23592 ?
Using TS 2.9.1,enum Test { A = "a", B = "b" } export interface PhaseApi1 { [x in Test]: () => void; }
still have issues
* A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type. * A computed property name must be of type 'string', 'number', 'symbol', or 'any'. * Cannot find name 'x'.
enum Test { A = "a", B = "b" } type PhaseApi1 = { [x in Test]: () => void; };
Reacted by Gyandeep Singh, Mikhail Shustov, Amit Beckenstein, SlurpTheo, Thijs Koerselman, Fabian Lauer, Artem Drobotenko, Andrew Boyton, hextraza, Ivan Shaban and 49 moreReacted by Amit Beckenstein, Nick Veys, Tim Kinnane, hextraza, Matt Dell, Kenneth Luplau-Brøgger, Cosmo, Vladimir Jimenez, Oleh, Javier Pacareu and 3 moreReacted by Amit Beckenstein, Nick Veys, Tim Kinnane, hextraza, ChenYong, Matt Dell, Teddy Toussaint, Kenneth Luplau-Brøgger, Vlad, dunxen and 5 moreReacted by Matt Dell, Oleksandr Havrashenko, Kenneth Luplau-Brøgger, Robert Molina, Aru Sahni and Andrii OriekhovReacted by Darek GreenlyJust a postmortem FYI for anyone who's gone through what I did, trying for hours to solve this with all manner of type rejigging to remove index signature errors... Make sure your IDE linter has the latest version of Typescript. Those errors might not actually be errors at all.
+1 for the 😭 reaction option.
Reacted by Guy Nesher and Alexander TrauzziReacted by AndrewMohamed Hegazy (@mhegazy) Any reason that this works for
typeand notinterface? I know it's easy to use one over the other but it seems a strange restriction.Reacted by Tommy Troy Lin, Sander Vispoel, Jordan Schroter, Shay Yaakov, Simon, Kon Stormozhev, Olli Varis, Timo Lins, Will Stone, Jan Buschtöns and 64 moreIt's unlikely that this happens to anyone else but me, but if you're trying to figure out why the fix isn't working for you, check that you spelled your enum correctly.
Reacted by Chase Holdren, Loïc Huvé, el-davo, Harshit Pathak, Filip Krawczyk, Simon Mulser, Nicolas Gramlich, Rilka Li, lauro figueiredo, Barys Malaichyk and 6 morekg-currenxie commented
on Apr 17, 2020 More actionsenum Test { A = "a", B = "b" } type PhaseApi1 = { [x in Test]: () => void; };
Is there a way to make either key optional?
Reacted by PauloChiriVulpes commented
on Apr 17, 2020 ContributorAuthorMore actionsKristian Gerardsson (@kg-currenxie)
type PhaseApi1 = { [x in Test]?: () => void; };
Reacted by Paulo, kfryauff-g, Felipe Rios, Ki Hong, Robert Katzki and juan-mendez-deel
TypeScript Version: 2.1.4
Expected behavior:
No errors will be thrown.
Actual behavior:
index in UserInterfaceElementthrows the errorType 'UserInterfaceElement' is not assignable to type 'string'This is a regression caused by #12425. The change makes sense, but there are very valid use cases for supporting enums in index signatures.
For another use case, in a project I'm working on, we use an enum to list all types of items. We have a function that takes an object with the enum values as keys, and data as values.
A workaround is to use
index: number, but then it's not enforced that you have to use a valid ItemType as a key.Original Issue: #2491