Const Enums

What

A const enum is a performance-optimized variant of an enum that is completely removed during the compilation process.

How

By adding the const keyword, you tell the compiler to replace all references to the enum with their literal values in the output JavaScript:

const enum Direction { North, South }
const dir = Direction.North; // Compiles directly to: const dir = 0;

Why

const enums are ideal when you want the benefits of named constants without the runtime overhead or extra code generated by regular enums. However, they come with limitations: they cannot have computed values, and they do not support any runtime reflection or reverse mapping.

Summary

Use const enum when performance and bundle size are your top priorities and you don’t need runtime object access.