So, are enums ok or not?

https://lemmy.ml/post/6273756

So, are enums ok or not? - Lemmy

I have googled this and even got to the second page, and there does not seem to be any kind of consensus. As far as I can tell, it’s a bad idea because it creates code you don’t see and accepts inputs that you wouldn’t want. And yet, many people seem to like them more than, say, const unions due to being easier to refactor in bulk. So what gives? Is this a case of IT people having very strong opinions on stuff that doesn’t matter? Or is there a technical reason for or against it?

Basically, you can use them if you want to, but every single case where you would use them would be better served with a union type.
Or by an enum class.
To add, turning everything into an enum can make the code nearly unreadable and hard to work with. This isn’t Rust, and there’s no performance gain for using enums over string unions.
Another good option is const objects, it’s a recommended alternative in the official docs. www.typescriptlang.org/docs/handbook/enums.html#o…
Handbook - Enums

How TypeScript enums work

Use enums when you need enums. Don’t use them when you don’t need them. What’s the issue?
You probably don’t ever need enums since the const keyword was added after enums and const objects handle enum use cases unions don’t.
You mean const assertions. Well, the thing is that const assertions are not enums. They don’t handle enum use cases in any way.
I’m talking about if you want an object syntax for accessing constant types to act as an enum www.typescriptlang.org/docs/handbook/enums.html#o…
Handbook - Enums

How TypeScript enums work

At work, when I was helping with some frontend stuff, we used object literals.

const DIRECTIONS = { UP: “UP”, DOWN: “DOWN” } as const;type DIRECTIONS = typeof DIRECTIONS[keyof typeof DIRECTIONS];

Taken from option 2 from this blog post. …medium.com/alternatives-to-typescript-enums-50e4…

This is, in my opinion, the absolute best way to do it

That’s what the typescript official docs recommend as an alternative too

www.typescriptlang.org/docs/handbook/enums.html#o…

The const keyword was added after enums. I doubt enums would exist if the feature was added earlier.

Handbook - Enums

How TypeScript enums work