Course contentsShow
← TypeScript
Lesson 522 of 1,79612. EnumsFree lesson

Bundle Size and Tree-Shaking

Why string-literal unions produce smaller bundles and why const enums partially address this.

Bundle Size and Tree-Shaking

You'll learn: Why string-literal unions produce smaller JavaScript bundles than enums, and how const enums help reduce enum overhead.

The Bundle Size Problem with Regular Enums

Remember that regular enums generate runtime JavaScript code — an object with both forward and reverse mappings. This code always ships to your users, even if they only need one enum value.

When you bundle your app with tools like Webpack or Vite, tree-shaking removes unused code. But because enums create objects with dynamic property access, bundlers can't safely remove them. The entire enum object stays in your bundle.

String-literal unions, by contrast, compile to plain strings. If you only use "pending" in your code, that's all that ships — just the literal string.

How String-Literal Unions Win

A union like type Status = "pending" | "complete" doesn't exist at runtime. TypeScript erases it during compilation. Only the actual string values you write ("pending", "complete") remain in your JavaScript.

Result: Smaller bundles and better tree-shaking.

How const Enums Partially Bridge the Gap

const enum members get inlined at compile time — replaced with their literal values. This eliminates the runtime object entirely, giving you enum-like syntax with union-like bundle size.

Trade-off: You lose reverse mapping, iteration, and some tooling compatibility, but you gain the performance benefits of literal values.

The Bottom Line

If bundle size matters (and it usually does in web apps), string-literal unions are the clear winner. They tree-shake perfectly and leave zero runtime footprint. const enum offers a middle ground — enum syntax with inline performance — but comes with limitations you must accept.

Key Takeaway: String-literal unions produce smaller bundles because they compile to plain values; const enums inline values to match this efficiency, but regular enums always add runtime overhead that can't be tree-shaken.