Sub topic 6
String Enums in TypeScript
This chapter assumes you know basic TypeScript types, function annotations, object literals, and switch statements. It focuses on string enums, how they behave at runtime, and when they are a better choice than a union of string literals.
What a string enum is
A string enum is a TypeScript enum whose members each have an explicit string value. An enum is a named group of related constants.
enum Color {
Red = "RED",
Green = "GREEN",
Blue = "BLUE",
}
function describeColor(color: Color): string {
switch (color) {
case Color.Red:
return "The color is red.";
case Color.Green:
return "The color is green.";
case Color.Blue:
return "The color is blue.";
}
}
console.log(describeColor(Color.Green));The enum creates names such as Color.Red and gives each name a stable value. The program prints:
The color is green.String values make the generated JavaScript and serialized data easier to understand than automatically assigned numbers.
String enums versus numeric enums
Numeric enum members can receive values automatically:
enum Priority {
Low,
Medium,
High,
}Here, Priority.Low is 0, Priority.Medium is 1, and Priority.High is 2. These numbers can be compact, but they are not meaningful when they appear in logs, JSON, or a database.
String enums require values:
enum Priority {
Low = "low",
Medium = "medium",
High = "high",
}| Property | Numeric enum | String enum |
|---|---|---|
| Values | Usually numbers | Explicit strings |
| Automatic values | Yes | No |
| Meaning in JSON | Less obvious | Usually clear |
| Reverse mapping | Available by default | Not available |
String enum values are useful at boundaries. For example, an API can receive "high" without requiring another system to know that High means 2.
Type checking and enum members
A function that expects Priority accepts enum members:
enum Priority {
Low = "low",
Medium = "medium",
High = "high",
}
function queueJob(priority: Priority): void {
console.log(`Queued with priority: ${priority}`);
}
queueJob(Priority.High); // OK
queueJob("high"); // ErrorAlthough the value of Priority.High is "high", TypeScript does not generally treat an arbitrary string literal as a Priority member. Use the enum member when the function requires the enum type.
This distinction gives enum values a named identity. It can prevent accidentally passing a string from an unrelated part of an application.
Runtime behavior
Enums are real JavaScript objects after compilation. A simplified version of the generated object looks like this:
var Color = {
Red: "RED",
Green: "GREEN",
Blue: "BLUE"
};You can use the object at runtime:
enum Status {
Active = "active",
Inactive = "inactive",
}
console.log(Status.Active);
console.log(Object.values(Status));The output is:
active
[ 'active', 'inactive' ]String enums do not provide numeric-style reverse mapping. With a numeric enum, Level[1] can return "Medium". With a string enum, Status["active"] does not automatically return "Active".
Choosing string enums or literal unions
A union of string literals is often simpler when you only need compile-time checking:
type Direction = "up" | "down" | "left" | "right";
function move(direction: Direction): void {
console.log(`Moving ${direction}`);
}
move("up");Choose a string enum when:
- You want named members such as
Status.Active. - The set must exist as a runtime object.
- You want one shared definition for code that reads or iterates over the values.
- Your project already uses enums consistently.
Choose a literal union when:
- Plain strings arrive from an API or configuration.
- You want lightweight emitted JavaScript.
- You need template literal types or easy composition with other string unions.
- Your tooling or library guidelines avoid enums.
A string enum adds a runtime object and requires callers to use enum members. A literal union disappears after compilation and accepts matching string literals directly.
Exercise
Exercise
Define a string enum named Format with members Json and Csv, using the values "json" and "csv". Then write a function that accepts only this enum.
Show answer
Use enum Format { Json = "json", Csv = "csv" } and declare the parameter as format: Format.