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

No Tree-Shaking for Enums

Bundlers struggle to tree-shake unused enum members because they're object properties, unlike unused string literals which disappear.

No Tree-Shaking for Enums

You'll learn: Why bundlers can't eliminate unused enum members from your production bundles, making enums heavier than string literals.

The Tree-Shaking Problem

When you bundle your TypeScript code for production, tools like Webpack, Rollup, or esbuild perform tree-shaking: they eliminate unused code to keep your bundle small. However, enums present a unique challenge.

Because TypeScript compiles enums into runtime objects with properties, bundlers treat them like any other object. If you import an enum and use even one member, the bundler must include the entire enum object in your bundle — it can't safely remove the unused properties.

Why This Happens

Think of it like ordering a pizza. With string literals, you only pay for the slices you eat. With enums, you must buy the whole pizza even if you only want one slice, because the restaurant (bundler) can't safely cut up the box after it's assembled.

String literals disappear completely during compilation — they're just inline values. Enums become real JavaScript objects that exist at runtime, and bundlers can't determine which properties might be accessed dynamically (via bracket notation or iteration).

The Impact

If you define an enum with 20 members but only use 2 in a particular module, all 20 members still end up in your bundle. With string literal unions, only the 2 values you actually reference would be inlined.

This is why many projects prefer string literal unions over enums for values that don't need reverse mapping or runtime reflection.

Key Takeaway: Enums compile to objects whose properties cannot be individually tree-shaken, making them heavier than string literal unions that disappear entirely at compile time.