Skip to content

Enums in TypeScript

Define named constant sets with numeric and string enums, and when to prefer alternatives.

What you'll learn

  • How to define numeric and string enums
  • The difference between enum members and their values
  • Reverse mapping in numeric enums
  • When to use string literal unions instead of enums

Concept

Numeric and String Enums

An enum is a way to define a set of named constants. A numeric enum assigns sequential numbers by default: enum Direction { Up, Down, Left, Right } gives Up=0, Down=1, etc. A string enum uses explicit string values: enum Status { Active = "ACTIVE", Inactive = "INACTIVE" }. String enums are generally preferred because the compiled output is self-documenting and immune to reordering.

How Enums Compile

Numeric enums generate a JavaScript object with both forward and reverse mappings — you can go from name to value and from value to name. String enums only generate forward mappings. This reverse mapping in numeric enums can be surprising and is another reason to prefer string enums. The compiled enum object exists at runtime, unlike type aliases which are erased entirely.

Enums vs Alternatives

For simple sets of string constants, a string literal union (type Status = "active" | "inactive") is often better than an enum: it has no runtime overhead, works naturally with switch statements, and produces cleaner compiled output. Use enums when you need the runtime object (e.g. iterating over values) or when you want the self-documenting nature of named enum members in your API.

Example

              // Numeric enum — values are 0, 1, 2, 3 by default.
enum Direction {
  Up = 0,
  Down = 1,
  Left = 2,
  Right = 3,
}

// String enum — explicit string values, preferred for clarity.
enum Status {
  Active = "ACTIVE",
  Inactive = "INACTIVE",
  Pending = "PENDING",
}

function handleStatus(status: Status): string {
  switch (status) {
    case Status.Active:
      return "User is active";
    case Status.Inactive:
      return "User is inactive";
    default:
      return "Unknown status";
  }
}

const current: Status = Status.Pending;
console.log(handleStatus(current));
console.log(Direction.Up, Direction.Left);
            

Numeric enums auto-assign numbers and support reverse mapping. String enums use explicit values and are clearer. Consider string literal unions for zero-runtime-cost alternatives.

Try it

  • JSON to TypeScript

    See how enum-like string fields in JSON become union types.

  • Code Minifier

    Observe how enums compile to runtime objects versus erased types.

Common mistakes

The mistake

Relying on numeric enum auto-incrementing and then reordering members

The fix

Reordering numeric enum members changes their values, which can break serialized data. Use string enums or explicitly assign numeric values to prevent accidental changes.

The mistake

Using const enum across package boundaries

The fix

Const enums are inlined at compile time and can break when consumed by a different package. Use regular enums or string literal unions for shared code.

Practice Enums in TypeScript

8 exercises

Practice Enums in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors