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

TypeScript

844 readers
1 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
[โ€“] Statick 5 points 1 year ago* (last edited 1 year 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 2 points 1 year ago

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

load more comments (1 replies)