TypeScript Enums vs. String Unions: What's the Deal?
Enums in TypeScript can be a bit of a double-edged sword. They look neat, give you nice tooling, and let you group related constants. But they also come with quirks, runtime implications, and compatibility oddities. If you've ever scratched your head wondering whether to use an enum or just go with string unions, this guide is for you. Let's break it all down. The Classic Enum Here's a classic numeric enum in TypeScript: enum Priority { low = 0, medium = 1, high = 2, critical = 3, } You can use it like this: const p = Priority.high; console.log(Priority[p]); // 'high'

Enums in TypeScript can be a bit of a double-edged sword. They look neat, give you nice tooling, and let you group related constants. But they also come with quirks, runtime implications, and compatibility oddities. If you've ever scratched your head wondering whether to use an enum
or just go with string unions, this guide is for you.
Let's break it all down.
The Classic Enum
Here's a classic numeric enum in TypeScript:
enum Priority {
low = 0,
medium = 1,
high = 2,
critical = 3,
}
You can use it like this:
const p = Priority.high;
console.log(Priority[p]); // 'high'