String Enums

What

String enums are enums where each member is explicitly initialized with a string literal.

How

Unlike numeric enums, you must provide a value for every member:

enum LogLevel {
  ERROR = "ERROR",
  WARN = "WARN",
  INFO = "INFO",
}

function log(msg: string, level: LogLevel) {
  console.log(`[${level}] ${msg}`);
}

Why

String enums are significantly easier to debug than numeric enums when data is serialized (e.g., sent over an API or stored in a database). Seeing "ADMIN" in a JSON response is much more helpful than seeing a cryptic number like 7. However, note that string enums do not support bidirectional mapping.

Summary

Use string enums when you want the underlying data to be human-readable outside of your TypeScript code.