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.
Enums vs const enum vs union-literal alternatives is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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); // 0String enum
String enums are readable in logs and APIs. They also emit runtime objects.
enum Status {
Ok = "ok",
Error = "error"
}
const s: Status = Status.Ok;
console.log(s); // "ok"const enum
const enum inlines values at compile time for zero runtime cost, but requires consistent build tooling.
const enum Axis {
X = 0,
Y = 1
}
const a: Axis = Axis.X;
console.log(a); // 0 (inlined)Union-literal alternative
Union-literals: no runtime output, great narrowing in switch, and excellent tree-shaking. Great default for many cases.
type Dir = "left" | "right";
function move(d: Dir) {
switch (d) {
case "left":
console.log("←");
break;
case "right":
console.log("→");
break;
}
}
move("left");When to choose
Guidelines:
- Prefer union-literals for most option sets (no runtime cost).
- Use enum if you need a runtime object or reverse mapping.
- Use const enum for inlined constants when your build supports it.
Union-literal vs enum check
Quick check: Which statement is TRUE about union-literal types as an alternative to enums?
Recap
Recap: enum creates runtime objects; const enum inlines values for zero cost; union-literals give safety with no runtime output. Choose based on your build and API needs.
Frequently asked questions
Is the “Enums vs const enum vs union-literal alternatives” lesson free?
Yes — the full text of “Enums vs const enum vs union-literal alternatives” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “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. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Enums vs const enum vs union-literal alternatives” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrays & ReadonlyArray
- Tuples & labeled tuples
- Enums vs const enum vs union-literal alternatives