3 ms·
This is way harder to parse and understand than the enum alternative. Personally I am definitely not skilled enough at typescript to come up with this on my ow
by akdev1l 2y ago
This is way harder to parse and understand than the enum alternative.
Personally I am definitely not skilled enough at typescript to come up with this on my own before seeing this thread so this was not even an option until now.
- mistercow 2y agoWell, that's basically how you would have done it in vanilla JS before typescript came around. The main awkwardness is the type definition. I often prefer to use a library like type-fest for this kind of thing so you can just say: export type MyEnum = ValueOf<MyEnumMapping>; TypeScript not having enough sugar in its built-in utility types is definitely a fair criticism. But more to the point, the above is not usually how you do enums in TS unless you have some very specific reason to want your values to be numbers at all times. There are some cases like that, but usually you would just let the values be strings, and map them to numbers on demand if that's actually required (e.g. for a specific serialization format).
- nosefurhairdo 2y agoYou get used to it! Also much easier to read in an editor with intellisense than your first exposure as plain text. If you're going to spend any considerable amount of time writing typescript, parent's advice is good.