Const Enums

Inlined at Compile Time

A const enum creates no runtime object. Each use is replaced by its value during compilation, so the output is smaller.

Example

const enum Direction {
  Up = "UP",
  Down = "DOWN",
}

const move: Direction = Direction.Up;
console.log(move);

Output

UP

Compiled JavaScript: the enum is gone and the value is inlined.

const move = "UP" /* Direction.Up */;
console.log(move);

Caution

Note: Const enums do not work well with isolatedModules (used by Babel, esbuild, SWC, Vite, Next.js) when they are exported across files, because those tools compile one file at a time and cannot see the values. Many teams avoid const enum and use literal unions instead.