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
Rather than introducing a new keyword of symbol these options either use inheritance style, or Generics, and would pave the way for arbitrary-valued enums?
Reacted by kgtkr, Wenlu Wang, Daniel Weiner, Summer Knight, Constantine Theocharis, Aaron J. Lang, Marko Harjula, Larry1123, resynth1943, Viktor Lazarev and 6 more
Is there something that would help move this issue?
If it would just be making a PR at this point I might be able to do it, may need to be guided in the right direction to start.
I've found an alternative way to do it, and I hope it helps:
/** * Action types for StoreState.todo */exportnamespaceTodoActionTypes{// Adding todo itemexportconstADD_ITEM=Symbol('@todo/ADD_ITEM')exporttypeADD_ITEM=typeofADD_ITEM// Removing todo itemexportconstDEL_ITEM=Symbol('@todo/DEL_ITEM')exporttypeDEL_ITEM=typeofDEL_ITEM// Performing undo on StoreState.todoexportconstUNDO=Symbol('@todo/UNDO')exporttypeUNDO=typeofUNDO// Performing redo on StoreState.todoexportconstREDO=Symbol('@todo/REDO')exporttypeREDO=typeofREDO}/** * Action types for StoreState.todo */exporttypeTodoActionTypes=|TodoActionTypes.ADD_ITEM|TodoActionTypes.DEL_ITEM|TodoActionTypes.UNDO|TodoActionTypes.REDO
I've found an alternative way to do it, and I hope it helps:
/** * Action types for StoreState.todo */exportnamespaceTodoActionTypes{// Adding todo itemexportconstADD_ITEM=Symbol('@todo/ADD_ITEM')exporttypeADD_ITEM=typeofADD_ITEM// Removing todo itemexportconstDEL_ITEM=Symbol('@todo/DEL_ITEM')exporttypeDEL_ITEM=typeofDEL_ITEM// Performing undo on StoreState.todoexportconstUNDO=Symbol('@todo/UNDO')exporttypeUNDO=typeofUNDO// Performing redo on StoreState.todoexportconstREDO=Symbol('@todo/REDO')exporttypeREDO=typeofREDO}/** * Action types for StoreState.todo */exporttypeTodoActionTypes=|TodoActionTypes.ADD_ITEM|TodoActionTypes.DEL_ITEM|TodoActionTypes.UNDO|TodoActionTypes.REDO
constCOLOR_RED=Symbol('COLOR_RED');constCOLOR_ORANGE=Symbol('COLOR_ORANGE');// intentionally not part of the enum for demonstrationconstCOLOR_GREEN=Symbol('COLOR_GREEN');// freeze needed for runtime protectionexportconstcolors=Object.freeze({COLOR_RED,COLOR_ORANGE,}asconst);typeValuesWithKeys<T,KextendskeyofT>=T[K];typeValues<T>=ValuesWithKeys<T,keyofT>exporttypeCOLOR=Values<typeofcolors>;functionfoo(val: COLOR){}foo(COLOR_RED);foo(colors.COLOR_ORANGE);foo(COLOR_GREEN);// refused by typescript
To continue on the same vein as this thread, I think enum values should be symbolic by default rather than integers. That's the whole point of enums to begin with. It would be best if developers don't even know the underlying value but rather treat enums as a union of interrelated Symbols. It would promote developers to have much better coding standards.
I have rediscovered same ideas guys people mentioned above and made small library just for this. No key name repetition needed as in Tomas' example, but still verbose.
Activity
[-]【SUGGESTIONS】symbol enum[/-][+]proposal:symbol enum[/+]mhegazy commentedon Sep 12, 2017
We first need a way to treat
symbols as literals, just like string and numeric literals. this is covered in #15473nisimjoseph commentedon Dec 23, 2017
I see #15473 merged, does it mean we can use Symbols in Enum somehow?
I want to use Enum with the Types: string, number or Symbol.
fbartho commentedon Jan 18, 2018
Not to bikeshed, but wouldn't a more consistent syntax be:
Rather than introducing a new keyword of
symbolthese options either use inheritance style, or Generics, and would pave the way for arbitrary-valued enums?DanielRamosAcosta commentedon Jun 27, 2018
This would be very useful with Redux actions...
Larry1123 commentedon Sep 12, 2019
Is there something that would help move this issue?
If it would just be making a PR at this point I might be able to do it, may need to be guided in the right direction to start.
littleclown commentedon Jun 2, 2020
I've found an alternative way to do it, and I hope it helps:
cosmoarunn commentedon Oct 3, 2020
i guess this will do?
polomsky commentedon Oct 20, 2020
I have found another solution at https://fettblog.eu/symbols-in-javascript-and-typescript/ and I slightly improved it. Sadly, there is lot of code around it, but it gives pretty protection (compile time and runtime)
alaboudi commentedon Apr 10, 2023
To continue on the same vein as this thread, I think enum values should be symbolic by default rather than integers. That's the whole point of enums to begin with. It would be best if developers don't even know the underlying value but rather treat enums as a union of interrelated Symbols. It would promote developers to have much better coding standards.
So something like
would be best if translated into something like
with keys used as the arguments in Symbol constructors to have google error reporting at runtime.
Somnium7 commentedon Aug 27, 2026
I have rediscovered same ideas guys people mentioned above and made small library just for this. No key name repetition needed as in Tomas' example, but still verbose.
Take a look if you're interested: ts-symbol-enum (npm)