Enums vs const enum vs union-literal alternatives
Compare classic enums, const enums, and union-literal alternatives; learn trade-offs for DX, size, and safety.
Intro
Goal: See when to use enum, const enum, or union-literal types. You'll compare runtime output, DX, and tree-shaking.
Numeric enum
Numeric enums emit a runtime object and allow reverse mapping. Simple, but they add some bundle size.
enum Direction {
Left = 0,
Right = 1
}
const d: Direction = Direction.Left;
console.log(d); // 0All lessons in this course
- Arrays & ReadonlyArray
- Tuples & labeled tuples
- Enums vs const enum vs union-literal alternatives