Hacker News new | past | comments | ask | show | jobs | submit
The suggested alternative looks overly complex to me. Moreover, it uses the `__proto__` property that is deprecated [0] and never was standardized. I could write something like this instead:

  type MyEnum = typeof MyEnum[keyof typeof MyEnum];
  const MyEnum = {
    A: 0,
    B: 1,
  } as const;
Unfortunately I found it still more verbose and less intuitive than:

  enum MyEnum {
    A = 0,
    B = 1,
  }
TypeScript enum are also more type-safe than regular union types because they are "nominally typed": values from one enum are not assignable to a variable with a distinct enum type.

This is why I'm still using TypeScript enum, even if I really dislike the generated code and the provided features (enum extensions, value bindings `MyEnum[0] == 0`).

Also, some bundlers such as ESbuil are able to inline some TypeScript enum. This makes TypeScript enum superior on this regard.

In a parallel world, I could like the latter to be a syntaxic sugar to the former. There were some discussions [1] for adopting a new syntax like:

  const MyEnum = {
    A: 0,
    A: 1,
  } as enum;
[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[1] https://github.com/microsoft/TypeScript/issues/59658

Re: __proto__, it's addressed in TFA

> Note that __proto__ also exists as a getter and a setter in Object.prototype. This feature is deprecated in favor of Object.getPrototypeOf() and Object.setPrototypeOf(). However, that is different from using this name in an object literal – which is not deprecated.

Thanks for the reply. I was not aware of this.

In this case, I could write this:

  type Activation = "Active" | "Inactive";
  const Activation = {
    __proto__: null,
    Active: "Active",
    Inactive: "Inactive",
  } as { [K in Activation]: K };
This completely hides `__proto__` and avoid using utility types like `Exclude`.

Note that it is safe because TypeScript checks that the type assertion is valid. If I mistype a value, TypeScript will complain about the assertion.