Enums allow you to define a set of named constants.

Numeric Enums

Default behavior - enum values are numbers starting from 0.

enum Direction {
  Up,      // 0
  Down,    // 1
  Left,    // 2
  Right   // 3
}

let direction: Direction = Direction.Up;
console.log(direction); // 0

Custom Numeric Values

enum Status {
  Pending = 1,
  Approved = 2,
  Rejected = 3
}

// Or start from a specific number
enum Status {
  Pending = 100,
  Approved,    // 101
  Rejected     // 102
}

String Enums

Each member must be initialized with a string.

enum Color {
  Red = "red",
  Green = "green",
  Blue = "blue"
}

let color: Color = Color.Red;
console.log(color); // "red"

Heterogeneous Enums

Mix of string and numeric values (not recommended).

enum Mixed {
  No = 0,
  Yes = "yes"
}

Computed and Constant Members

enum FileAccess {
  None,           // 0 (constant)
  Read = 1 << 1,  // 2 (computed)
  Write = 1 << 2, // 4 (computed)
  ReadWrite = Read | Write // 6 (computed)
}

const Enums

Inlined at compile time - no JavaScript object is generated.

const enum Direction {
  Up,
  Down,
  Left,
  Right
}

let direction = Direction.Up;
// Compiles to: let direction = 0;

Reverse Mapping

Numeric enums create reverse mappings (not for string enums).

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

console.log(Direction.Up);        // 1
console.log(Direction[1]);        // "Up" (reverse mapping)

When to Use Enums

Good Use Cases

  1. Fixed set of constants
enum HttpStatus {
  OK = 200,
  NotFound = 404,
  ServerError = 500
}
  1. Flags/Bitwise operations
enum Permission {
  Read = 1,
  Write = 2,
  Execute = 4
}
  1. API response codes
enum ApiResponse {
  Success = "SUCCESS",
  Error = "ERROR",
  Pending = "PENDING"
}

Alternatives to Enums

  1. Union Types (often preferred)
type Direction = "up" | "down" | "left" | "right";
  1. const objects
const Direction = {
  Up: "up",
  Down: "down",
  Left: "left",
  Right: "right"
} as const;

Exercises

Exercise 1: Numeric Enum

// Create an enum called 'Priority' with:
// - Low = 1
// - Medium = 2
// - High = 3
// - Critical = 4

Exercise 2: String Enum

// Create an enum called 'UserRole' with:
// - Admin = "admin"
// - User = "user"
// - Guest = "guest"

Exercise 3: Enum Functions

// Create an enum 'HttpMethod' and a function that:
// - Takes an HttpMethod
// - Returns true if it's a safe method (GET, HEAD, OPTIONS)

Exercise 4: Enum with Flags

// Create an enum 'Permission' with bitwise flags:
// - Read = 1
// - Write = 2
// - Delete = 4
// Create functions to check and combine permissions

Best Practices

  1. Use string enums for better debugging - They preserve values in runtime
  2. Use const enums for performance - When you don't need runtime object
  3. Consider union types - Often simpler than enums
  4. Avoid heterogeneous enums - They're confusing
  5. Use enums for fixed sets - When values won't change

Common Mistakes

  1. Using enums when union types would work
// ❌ Overkill
enum Status {
  Active,
  Inactive
}

// ✅ Simpler
type Status = "active" | "inactive";
  1. Not initializing string enum members
// ❌ Error
enum Color {
  Red,  // Error: Enum member must have initializer
  Green,
  Blue
}

// ✅ Good
enum Color {
  Red = "red",
  Green = "green",
  Blue = "blue"
}
  1. Expecting reverse mapping in string enums
enum Direction {
  Up = "up"
}

// Direction["up"]; // ❌ undefined - no reverse mapping for string enums

Next Steps

Learn about Type Assertions.