Course contentsShow
← TypeScript
Lesson 490 of 1,79612. EnumsFree lesson

Basic String Enum Syntax

Define a string enum where each member requires an explicit string literal value.

Basic String Enum Syntax

What you'll learn: How to define a string enum where every member must have an explicit string value.


The Core Idea

Unlike numeric enums (which auto-increment), string enums require you to manually assign a string literal to every single member. You cannot leave any member uninitialized — TypeScript won't guess or generate values for you.

The syntax looks like this:

enum Direction {
  Up = "UP",
  Down = "DOWN",
  Left = "LEFT",
  Right = "RIGHT"
}

Each member (Up, Down, etc.) gets an explicit string literal on the right side of the = sign. No exceptions.


Why Explicit Values Matter

String enums are completely opt-in for their values. TypeScript doesn't auto-increment them (like 0, 1, 2... with numeric enums) because strings don't have an obvious "next" value. This makes string enums self-documenting — the runtime value matches what you write, making debugging and logging clearer.


Using a String Enum

enum Status {
  Pending = "PENDING",
  Success = "SUCCESS",
  Error = "ERROR"
}

let currentStatus: Status = Status.Pending;

console.log(currentStatus); // Logs: "PENDING"

At runtime, Status.Pending evaluates to the string "PENDING", exactly as you defined it.


What Happens If You Forget?

enum Color {
  Red = "RED",
  Green // ❌ Error: Enum member must have initializer
}

TypeScript will complain immediately. Every member needs a value.


Key Takeaway: String enums require explicit string literal values for every member — no auto-generation, guaranteeing readable, predictable runtime values.