Course contentsShow
← TypeScript
Lesson 486 of 1,79611. Literal TypesFree lesson

When to Graduate to Real Enums

Identify scenarios where numeric enums or const enums provide better ergonomics than literal unions.

When to Graduate to Real Enums

What you'll learn: How to recognize when numeric or const enums offer better ergonomics than literal unions for your specific use case.

You've been working with string literal unions like type Status = "pending" | "active" | "complete", which are lightweight and type-safe. But TypeScript also has actual enums — a dedicated language feature. So when should you switch?

The Strengths of Literal Unions

Literal unions excel at:

  • Simplicity: Just strings or numbers, no special syntax
  • Transparency: What you see is what you get at runtime
  • Tree-shaking: Unused members disappear in bundles
  • as const patterns: Easy to derive from objects

When Real Enums Make More Sense

Numeric Identity Over String Values

If you're mapping to numeric codes (like HTTP status codes, database IDs, or flag bits), numeric enums are cleaner:

// With literals: verbose and error-prone
type StatusCode = 200 | 201 | 400 | 404 | 500;

// With enum: self-documenting
enum StatusCode {
  OK = 200,
  Created = 201,
  BadRequest = 400,
  NotFound = 404,
  ServerError = 500
}

const response = StatusCode.OK; // 200, but with clear intent

Reverse Mapping Needs

Numeric enums automatically provide reverse lookups — you can get the name from the value:

enum Priority {
  Low = 1,
  Medium,
  High
}

const level = Priority.High; // 3
const name = Priority[3]; // "High"

This is impossible with literal unions alone.

Const Enums for Zero-Runtime Cost

If you want enum-like ergonomics but no JavaScript output, const enum inlines values at compile time — perfect for performance-critical code or libraries.

Key Takeaway: Graduate to enums when you need numeric mappings, reverse lookups, or compile-time inlining. Stick with literal unions for simple string-based discriminators where runtime transparency matters most.