this post was submitted on 10 Oct 2023
7 points (88.9% liked)

TypeScript

809 readers
1 users here now

founded 1 year 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?

top 12 comments
sorted by: hot top controversial new old
[–] spartanatreyu@programming.dev 9 points 11 months 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.

[–] pm_me_your_quackers@lemmy.world 4 points 11 months 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.

[–] LeafOnTheWind@lemmy.world 2 points 11 months ago

Or by an enum class.

[–] fidodo@lemmy.world 2 points 7 months 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

[–] Aux@lemmy.world 6 points 11 months ago (1 children)

Use enums when you need enums. Don't use them when you don't need them. What's the issue?

[–] fidodo@lemmy.world 1 points 7 months ago (1 children)

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.

[–] Aux@lemmy.world 1 points 7 months ago (1 children)

You mean const assertions. Well, the thing is that const assertions are not enums. They don't handle enum use cases in any way.

[–] fidodo@lemmy.world 1 points 7 months ago

I'm talking about if you want an object syntax for accessing constant types to act as an enum https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums

[–] Statick@programming.dev 5 points 11 months ago* (last edited 11 months ago) (2 children)

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 in this blog post. https://maxheiber.medium.com/alternatives-to-typescript-enums-50e4c16600b1`___`

[–] Fisherswamp@programming.dev 2 points 11 months ago

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

[–] fidodo@lemmy.world 2 points 7 months ago

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

https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums

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

[–] bobby_hill@lemmy.world 3 points 11 months ago