this post was submitted on 10 Oct 2023
9 points (90.9% liked)

TypeScript

910 readers
37 users here now

founded 2 years ago
MODERATORS
 

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?

you are viewing a single comment's thread
view the rest of the comments
[–] spartanatreyu 9 points 1 year ago (3 children)

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.

[–] [email protected] 4 points 1 year ago

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.

[–] [email protected] 2 points 1 year ago

Or by an enum class.

[–] [email protected] 2 points 1 year ago

Another good option is const objects, it's a recommended alternative in the official docs. https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums