Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A string enum in TypeScript is a strongly typed collection of named constants where each member is explicitly initialized with a string literal. Unlike numeric enums, string enums do not auto-increment and require explicit assignment for every member, as strings lack a mathematical progression from which the compiler can infer subsequent values.
enum Direction {
  Up = "UP",
  Down = "DOWN",
  Left = "LEFT",
  Right = "RIGHT"
}

Nominal Typing Behavior

String enums enforce strict nominal typing rather than structural typing. Even if a raw string literal perfectly matches the underlying value of an enum member, the TypeScript compiler will reject the assignment. You must access the value through the enum object itself.
enum Status {
  Success = "SUCCESS",
  Failure = "FAILURE"
}

let currentStatus: Status;

currentStatus = Status.Success; // Valid
currentStatus = "SUCCESS";      // Compiler Error: Type '"SUCCESS"' is not assignable to type 'Status'.

Runtime Representation and Compilation

At runtime, standard TypeScript string enums are transpiled into JavaScript objects using Immediately Invoked Function Expressions (IIFEs).
// Transpiled JavaScript output for standard enum
var Status;
(function (Status) {
    Status["Success"] = "SUCCESS";
    Status["Failure"] = "FAILURE";
})(Status || (Status = {}));
To optimize runtime performance and reduce bundle size, TypeScript provides const enums. When a string enum is declared with the const modifier, the compiler completely erases the IIFE object representation. Instead, it inlines the string literal values directly at every call site during compilation.
const enum HttpMethod {
  Get = "GET",
  Post = "POST"
}

let method = HttpMethod.Get;
// Transpiled JavaScript output for const enum
let method = "GET" /* HttpMethod.Get */;

Absence of Reverse Mapping

A critical mechanical difference between numeric and string enums is the absence of reverse mapping. Numeric enums generate a bidirectional map (mapping the name to the value, and the value back to the name). String enums only generate a unidirectional map. Using the standard Status enum above, the resulting JavaScript object at runtime contains only the forward mappings:
console.log(Object.keys(Status)); 
// Output: ["Success", "Failure"]
// Note: "SUCCESS" and "FAILURE" are not keys in the object.

Heterogeneous Enums

TypeScript permits mixing string and numeric members within the same enum, known as a heterogeneous enum. However, this practice introduces inconsistent runtime behavior regarding reverse mapping (numeric members will reverse map, string members will not). While fully supported by the TypeScript compiler, heterogeneous enums are generally avoided by developers and linters as a best practice due to this structural inconsistency.
enum MixedEnum {
  No = 0,
  Yes = "YES"
}
Master TypeScript with Deep Grasping Methodology!Learn More