TypeScript has added the erasableSyntaxOnly flag, that doesn't let you use Enums.
Have looked for several alternative options. Like:
What's the best practice to implement an alternative to TypeScript `enum`?
https://maxheiber.medium.com/alternatives-to-typescript-enums-50e4c16600b1
My best solution yet, for type safety and code completion is object literals with type-definition:
export const Environment = {
Development: 'development',
Production: 'production',
Test: 'test',
}
export type Environment = typeof Environment[keyof typeof Environment];
But, it's not pretty and looks hacky, especially the last line:
- Why do we need to add this convoluted line, just to get the type?
- Is
Environmenta type? A value? Both? This is not clear and error prone.
Is there a recommended way to migrate away from Enums? Haven't seen one in the docs
const Environmentconst. So} as const. The benefit of not using enums is that you wouldn't need explicitly import the enum everywhere where you need to compare the value, because you can 'just' use enter the value directly and it is statically checked.Environmenta type? A value? Both? This is not clear and error prone."—It's exactly the same as what you get with theenumkeyword (there is both a type and a value with the same name). Theclasskeyword also behaves this way.as enumproposal.