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

The const Enum Escape Hatch Trade-offs

Const enums inline values to avoid runtime cost but break when used across module boundaries or with isolatedModules enabled.

The const Enum Escape Hatch Trade-offs

You'll learn how const enum solves performance problems but introduces cross-module and tooling compatibility issues.

The Dual Nature of const enums

You've already learned that const enums inline their values at compile time to eliminate runtime overhead. This seems perfect — until you try using them across module boundaries or with certain build tools.

The problem is this: when TypeScript inlines a const enum value, it replaces references with literal values during compilation. But what happens when:

  • Another module imports your const enum?
  • You're using isolatedModules (which many modern bundlers require)?
  • A library consumer doesn't have access to your TypeScript source?

In these scenarios, const enums break.

The Trade-off

Think of const enums like instant coffee packets. They're incredibly convenient when you're making coffee alone in your kitchen (single module). But if you're trying to share them with a café that needs to brew in bulk (external modules), they don't work — the café needs real coffee beans (regular enums).

With isolatedModules: true (used by Babel, esbuild, swc):

// ❌ Error with isolatedModules
export const enum Status {
  Active = "ACTIVE",
  Inactive = "INACTIVE"
}

Across modules without source:

// library.ts
export const enum Color { Red, Blue }

// consumer.ts (compiled separately)
import { Color } from 'library';
const c = Color.Red; // ❌ Can't inline without source

The Escape Hatch

The preserveConstEnums flag tells TypeScript to also emit runtime code for const enums, giving you both inlining benefits and cross-module safety — but at the cost of reintroducing the runtime overhead you were trying to avoid.

Key Takeaway: const enum offers zero runtime cost through inlining, but breaks in isolated module compilation and cross-package scenarios unless you enable preserveConstEnums, which defeats the original purpose.