Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Const

enum

Enums define named constants and a type for a finite set of cases. TypeScript supports numeric, string, regular, and const enums, with regular enums available at runtime and const-enum members inlined during compilation.

How it works

An enum declaration creates a named set of constants and a corresponding type. Members may have constant or computed values; regular enums remain runtime objects, while const enums accept only constant enum expressions and are removed during compilation with member uses inlined.

Syntax

enum EnumName {
  Member [= value],
  ...
}

const enum EnumName {
  Member [= constantValue],
  ...
}

Key points

  • Use an enum to give a stable name to a finite set of distinct constant cases.
  • Numeric members can auto-increment from a starting value; string members use explicitly assigned string values.
  • The declaration form starts with enum, followed by the enum name and a brace-delimited member list.
  • Member values can be compile-time constants or expressions whose values are computed.
  • Read a member through property access such as Direction.Up, and use the enum name in a type annotation.
  • Const-enum references are replaced with their values where used instead of requiring a runtime lookup.

Common mistakes

  • A const enum cannot contain computed members; every member value must be a constant enum expression.
  • Regular enums emit runtime objects, so they add JavaScript output and can be accessed as values.
  • For the names of enum members as string keys, use keyof typeof Enum rather than keyof Enum.
  • An uninitialized member must be first or follow a suitable numeric constant member; it cannot follow an arbitrary computed value.

Example: Numeric enum with auto-incremented members

enum Direction {
  Up = 1,
  Down,
  Left,
  Right,
}

const direction: Direction = Direction.Down;
console.log(direction);

Defines a numeric enum whose later members continue from the explicitly assigned value, then uses a member as a value and as the declared type.

When to use it

Named domain cases

Representing a finite domain such as direction, status, or mode.

Self-documenting APIs

Making intent explicit when values correspond to distinct alternatives.

Erased compile-time constants

Avoiding a generated runtime enum object when compile-time constants are sufficient.

Interview questions

QWhat does an enum declaration provide?

An enum defines named constants and also creates a type with those members. Numeric enums can infer later values from an initialized numeric member, while string enums require explicit values.

Follow-up: How can you obtain the enum's member names as a type? Use keyof typeof Enum, because keyof applied directly to an enum does not produce the expected string-key union.

QHow do regular and const enums differ at runtime?

A regular enum is represented by a runtime object, so member access works in emitted JavaScript. A const enum is restricted to constant enum expressions, erased during compilation, and its members are substituted at use sites.

Follow-up: What trade-off follows from const-enum erasure? Code cannot rely on a const enum object existing at runtime.

QWhat are constant and computed enum members?

Enum members may be constant or computed. Constant enum expressions can be evaluated during compilation; const enums are limited to those expressions, whereas regular enums can also contain computed members.

Follow-up: What happens after an enum member has a computed value? Subsequent uninitialized members cannot automatically continue unless the preceding value is a valid numeric constant context.

  • const enum
  • numeric enums
  • string enums
  • enum members
  • keyof typeof

Source: TypeScript: Handbook - Enums. Checked 2026-10-15.