← Back to TypeScript Course | Chapter 19: Enums Deep Dive | Lesson 3 of 6

Const Enums

A const enum is designed to be inlined by the TypeScript compiler instead of generating a normal runtime enum object. This can reduce generated JavaScript, but it has build-tool and library-distribution considerations.
Syntax
typescript
const enum EnumName {
  Member1,
  Member2,
}
let value = EnumName.Member1;

Basic Const Enum

A const enum is declared with the const modifier and is normally inlined directly into its member value at compile time rather than generating a runtime enum object at all.

Example: Basic Const Enum

typescript
const enum Direction {
  Up,
  Down,
}
const d = Direction.Up;
console.log(d);

Const Enum with Functions

Const enum members work as typed function arguments the same way as normal enum members, but using them avoids creating an actual runtime enum object in the compiled output.

Example: Const Enum with Functions

typescript
const enum Color { Red, Green, Blue }
function paint(color: Color) {
  return `Painting with color ${color}`;
}
console.log(paint(Color.Green));

Const Enum and Compilation

Const enums are a compile-time-only feature — their members are substituted directly into the generated JavaScript, so there's no object you could inspect or iterate over at runtime the way you can with a regular enum.

Example: Const Enum and Compilation

typescript
const enum Size { Small, Medium, Large }
// At compile time, Size.Medium is inlined directly as the number 1.
console.log(Size.Medium);

Const Enum Limitations

Because const enums are meant to be inlined, code shouldn't rely on a runtime enum object existing — this becomes especially important when publishing a library, since some build tools handle const enums differently or disallow them entirely (isolatedModules).

Example: Const Enum Limitations

typescript
const enum Mode { Dev, Prod }
// No runtime object exists to iterate over -- Mode.Dev is just inlined as 0.
console.log(Mode.Dev);

When to Use Const Enums

Const enums suit internal applications where the team controls the whole toolchain and predictable inlining behavior is known, reducing the number of emitted enum objects in the final bundle.

Example: When to Use Const Enums

typescript
const enum Env {
  Development,
  Production,
}
console.log(Env.Production);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a const enum with isolatedModules or Babel, which can cause errors since they cannot inline across files.
  2. Trying to loop over a const enum with Object.keys, since no runtime object exists.
  3. Expecting to see the enum in the compiled JavaScript, when its values are inlined.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.