3 ms·
I don’t understand all these comments. I use TS enums like I use Java enums and I literally never have issues. What are y’all doing with these?
by Aeolun 2y ago
I don’t understand all these comments. I use TS enums like I use Java enums and I literally never have issues. What are y’all doing with these?
- s900mhz 2y agoSame, I have use enum strings for years and never had an issue.
- shortrounddev2 2y agoIn typescript, some types are values. Typescript treats enums as types, but they're secretly values. Classes are types and values.
- ARandumGuy 2y agoHow does that affect things in real code bases? I'm with Aeolun on this. I work with Typescript enums all the time, and have never encountered any issues. Maybe the other options work slightly better. But I struggle to see how Typescript enums could cause any problems when used like I'd use enums in any other language.
- Kiro 2y agoYeah, people arguing against enums in this thread are doing a really bad job. I haven't seen a single compelling argument.
- y-c-o-m-b 2y agoI agree; I'm in FAANG and it's even encouraged in my group. They are super easy to construct and work with and we've yet to encounter any side effects of using them despite our millions of customers world-wide. Maybe people should become familiar with Grug: https://grugbrain.dev/ https://grugbrain.dev/ I'll take the t-rex. > apex predator of grug is complexity. complexity bad. say again: complexity very bad. you say now: complexity very, very bad. given choice between complexity or one on one against t-rex, grug take t-rex: at least grug see t-rex
- pavel_lishin 2y agoI've had issues with enums on at least three occasions, but I cannot remember what they actually were - but every time I've tried using them, I've regretted it.
- sirlone 2y agoI can't remember the last time I needed enums. Usually they fall out of the code in one way or another // in some util.ts or helpers.ts export function keysOf<T extends string>(obj: { [k in T]: unknown }): readonly T[] { return Object.keys(obj) as unknown[] as T[]; } //--- const kOperationsFunctions = { add(a: number, b: number) { return a + b; }, subtract(a: number, b: number) { return a - b; }, multiply(a: number, b: number) { return a * b; }, divide(a: number, b: number) { return a / b; }, }; const kOperations = keysOf(kOperationsFunctions); type Operation = (typeof kOperations)[number]; I don't want to declare an enum here. That would just be a waste of typing and an opportunity for things to get out of sync.