-
Notifications
You must be signed in to change notification settings - Fork 1
typescript
enum TrafficLight {
Red,
Yellow,
Green
}
const currentLight: TrafficLight = TrafficLight.Red;
const instructions = new Map<TrafficLight, string>([
[TrafficLight.Red, "Stop"],
[TrafficLight.Yellow, "Slow down"],
[TrafficLight.Green, "Go"],
]);
console.log(instructions.get(currentLight));Here, the enum values themselves are symbolic. They aren’t used directly in the UI, APIs, or persisted anywhere—they're just internal tags for logic flow. If you want to serialize the enum value or display it to a user, that should be an explicit translation step (like the Map above).
https://yazanalaboudi.dev/in-defence-of-typescript-enums
COUNTER ARGUMENT:
why not use a union of literals (e.g. "red" | "yellow" | "green") instead?
Yeah, the fact that enums actually compile to code is the much bigger problem than misuse. Node now has support for TypeScript, but notably not enums, precisely because they can’t just be stripped to get valid JavaScript. IIRC deno has a configuration knob to forbid TypeScript features that cannot simply be stripped, including enums. Ideally TypeScript itself would deprecate enums.