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...> 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.
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.