Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

The const enum declaration

Reference for TypeScript const enum: how member values are inlined at compile time, what that removes, and why isolatedModules and erasable syntax limit it.

A const enum is an enum whose declaration produces no JavaScript object. Every use of a member is replaced by its literal value at compile time, so Color.Red becomes 0 in the output (usually with a comment naming the member). The declaration itself vanishes.

That makes the output smaller and the access faster, and it also means you cannot treat the enum as a value: no iterating, no reverse lookup, no passing the whole enum to a function.

TypeScript
const enum Color {
  Red,
  Green,
}
const c = Color.Green; // emitted as 1

Try it

TypeScript Members are inlined

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript No runtime object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
const enum Name {
  A,
  B = 2,
  C = "text",
}
declare const enum Ambient { X = 1 }
Part Description
Members Must be constant expressions: literals, earlier members, and simple arithmetic or bitwise operations on them. Computed values such as function calls are an error.
declare const enum Describes an enum from a declaration file. The values are baked into your output, so the library version must match.

Edge cases and errors:

  • A const enum can be used only in property-access form (Name.Member or Name["Member"]). Using the name as a value is error TS2475, and indexing with a non-literal (Weekday[1]) is error TS2476.
  • The reverse mapping that regular numeric enums have is not generated.
  • With isolatedModules (and verbatimModuleSyntax), referring to an ambient const enum from another file is restricted, because a single-file compiler cannot know its values.
  • preserveConstEnums keeps the object in the output even though uses are still inlined.

Usage notes

  • const enum was designed for a whole-program compiler. Tools that transpile each file alone (Babel, esbuild, swc, Node type stripping) cannot inline values from other files, which is why many projects ban it.
  • Publishing a library that exports a const enum is risky: consumers inline today's values, so renumbering a member later breaks them silently.
  • For a plain object-like lookup that you can iterate, use a regular enum or an as const object instead.
  • A regular enum with only constant members behaves the same at the type level; the only difference is the emitted code.
  • Under erasableSyntaxOnly, both enum and const enum are rejected.