My colleague @mattcosta7 demonstrated something that feels obvious in hindsight.
Instead of enums, use a regular object. So instead of
enum State {
SOLID,
LIQUID,
GAS,
}
(playground here - note how "complex" the transpiled JavaScript becomes)
...use an object. Objects have the advantage that when the TypeScript is converted to JavaScript, it looks pretty much identical. TypeScript 5.8 makes it possible to disallow "non-erasable syntax" which means you can set up your tsconfig.json
to avoid enum
.
The alternative is an object. It's a bit more verbose but it has advantages:
const State = {
SOLID: "solid",
LIQUID: "liquid",
GAS: "gas"
} as const
type State = typeof State[keyof typeof State]
(playground here - note how simple the transpiled JavaScript is)
In the above code, if you hover the mouse over State
it'll say
'solid' | 'liquid' | 'gas'