News for hackers. Stuff that matters.
Re:TypeScript enums: use cases and alternatives by mistercow on Monday January 20, 2025 @06:50AM (#42767717)
What in your view is the downside to doing this?

    export const MyEnumMapping = {
      active: 0,
      inactive: 1
    } as const

    export type MyEnum = typeof MyEnumMapping[keyof typeof MyEnumMapping];
So you have the names exposed, but the underlying type is the number.
Re:TypeScript enums: use cases and alternatives by akdev1l on Monday January 20, 2025 @08:22AM (#42768454)
This is way harder to parse and understand than the enum alternative.

Personally I am definitely not skilled enough at typescript to come up with this on my own before seeing this thread so this was not even an option until now.

Re:TypeScript enums: use cases and alternatives by nosefurhairdo on Monday January 20, 2025 @11:23AM (#42770167)
You get used to it! Also much easier to read in an editor with intellisense than your first exposure as plain text. If you're going to spend any considerable amount of time writing typescript, parent's advice is good.
Re:TypeScript enums: use cases and alternatives by mistercow on Monday January 20, 2025 @08:43AM (#42768646)
Well, that's basically how you would have done it in vanilla JS before typescript came around. The main awkwardness is the type definition. I often prefer to use a library like type-fest for this kind of thing so you can just say:

    export type MyEnum = ValueOf<MyEnumMapping>;
TypeScript not having enough sugar in its built-in utility types is definitely a fair criticism.

But more to the point, the above is not usually how you do enums in TS unless you have some very specific reason to want your values to be numbers at all times. There are some cases like that, but usually you would just let the values be strings, and map them to numbers on demand if that's actually required (e.g. for a specific serialization format).

Re:TypeScript enums: use cases and alternatives by eeue56 on Monday January 20, 2025 @10:08AM (#42769464)
I would do this instead:

  type MyEnum = {
    active: 0;
    inactive: 1;
  }

  const MyEnum: MyEnum = {
    active: 0, 
    inactive: 1,
  }

  const showAge = MyEnum.active;
  const showPets = MyEnum.inactive;

It's slightly more duplication, but a lot more readable (imo) to those unfamiliar to utility types. TypeScript also enforces keeping them in sync.
Re:TypeScript enums: use cases and alternatives by mistercow on Monday January 20, 2025 @11:23AM (#42770162)
That doesn't give you a type that you can use for the actual enum values. If you wanted a function argument to take in one of your enum values, you'd still have to use keyof in the signature like:

   function doSomethingWithMyEnum(val: MyEnum[keyof MyEnum])
You could do `val: number`, but now you're allowing any number at all.

Ultimately, the type syntax in TypeScript is a key part of the language, and I don't think it's unreasonable to expect developers to learn the basic typeof and keyof operators. If we were talking about something wonkier like mapped types or conditional types, sure, it might make sense to avoid those for something as basic as enums.

Re:TypeScript enums: use cases and alternatives by Kiro on Monday January 20, 2025 @11:36AM (#42770339)
Is this what you're referring to when you're talking about more elegant alternatives? Come on. You're not going to convince anyone with this.
Re:TypeScript enums: use cases and alternatives by mistercow on Monday January 20, 2025 @11:41AM (#42770384)
No, this is what I suggest for someone who wants to do something non-idiomatic because they're used to C. What I suggest in most cases is just a string union type.

Edit: But for what it's worth, yes, the above is still more elegant than enums. The syntax may feel less elegant, but among other things, the above does not depart from the structural type paradigm that the rest of the language uses, all for something as simple as an enum.

Re:TypeScript enums: use cases and alternatives by cap11235 on Monday January 20, 2025 @02:41PM (#42772272)
Typescript devs love doing anything except developing scripts with types