Sub topic 17
Enum compatibility
Prerequisites
This chapter assumes you know the assignability relation (source type and target type), literal types, and unions. It also assumes you have used a TypeScript enum declaration. Enums are a special case in the compatibility rules. They behave like structural types in some places and like nominal types in others.
Enums as types
An enum declaration creates a value at runtime and a type at compile time. The enum type behaves as a union of its member types, which are literal types. Since TypeScript 5.0, every enum is treated this way.
enum Direction {
Up = "UP",
Down = "DOWN",
}
const up: Direction.Up = Direction.Up;
const dir: Direction = up; // OK: Direction.Up is a member of the union
const bad: Direction.Up = Direction.Down; // Error: Direction.Down is not Direction.UpSo Direction is roughly Direction.Up | Direction.Down. Assigning a member to the enum type works because a union accepts each of its constituents. Assigning the whole enum to a single member type fails, because the source could be the other member.
Numeric enums and `number`
A numeric enum is assignable to number. A value of type number is also assignable to a numeric enum type.
enum Status {
Active, // 0
Inactive, // 1
}
const n: number = Status.Active; // OK
const s: Status = Status.Inactive; // OK
function fromNumber(value: number): Status {
return value; // OK: the type number is assignable to a numeric enum
}
const literalOk: Status = 1; // OK: 1 is the value of Status.Inactive
const literalBad: Status = 5; // Error in TypeScript 5.0 and later: no member has the value 5The two-way rule with number is deliberate. Numeric enums are often used as bit flags, and combining them with | produces a plain number.
enum Permission {
Read = 1,
Write = 2,
Execute = 4,
}
const readWrite: Permission = Permission.Read | Permission.Write; // OK: the result is numberIf number were not assignable to the enum, this common pattern would need a cast.
There is a hole in this rule. TypeScript 5.0 checks literal values against the enum's members. It does not check an arbitrary number variable, because it cannot know the value at compile time. A function that accepts Status can therefore still receive 42 through a number variable.
String enums are stricter
A string enum is assignable to string. The reverse is not allowed, not even for a string literal that matches a member's value.
enum Direction {
Up = "UP",
Down = "DOWN",
}
const str: string = Direction.Up; // OK
const d1: Direction = Direction.Up; // OK
const d2: Direction = "UP"; // Error: Type '"UP"' is not assignable to type 'Direction'String enum members get a nominal-style identity from their declaration. The literal "UP" is a different type from Direction.Up, even though both hold the same text at runtime. Callers must write Direction.Up, or you can accept a union of string literals instead (see the last section).
Different enums are never compatible with each other
Two enum types are not assignable to each other, even when their members have the same names and values.
enum E1 {
X,
}
enum E2 {
X,
}
const n: number = E1.X; // OK: numeric enums are assignable to number
const e: E1 = E2.X; // Error: E2.X is not assignable to E1This is one of the few places where TypeScript ignores shape. Both enums have a member X with value 0, so they are structurally identical. The checker still rejects the assignment, so the declaration is what counts.
Enums with the same name
There is one exception to the rule that different enums are incompatible. If two enum declarations have the same name, and every member name in the source enum also exists in the target enum, TypeScript treats them as compatible. This exists for the case where two copies of one package, at different versions, each declare their own enum Color. Without the exception, a value from one copy could not be passed to a function from the other. The check compares names, so do not rely on it to separate two unrelated enums that happen to share a name.
Comparing enums with other values
Assignment is not the only check. The === operator and type assertions use the comparability relation, which is more lenient than assignability. Comparing a numeric enum value with a number is allowed. Comparing a string enum value with a plain string literal that has no matching member can produce an error such as "This comparison appears to be unintentional because the types have no overlap". If you see that error, check whether you are comparing against a value that is not a member of the enum.
Narrowing and exhaustiveness
Because an enum behaves like a union, a switch narrows it member by member. After all members are handled, the remaining type is never. Since never is assignable to everything, this gives a compile-time exhaustiveness check.
enum Direction {
Up = "UP",
Down = "DOWN",
}
function describe(d: Direction): string {
switch (d) {
case Direction.Up:
return "going up";
case Direction.Down:
return "going down";
default: {
const unreachable: never = d; // Error here if a member is added and not handled
return unreachable;
}
}
}If you add Left = "LEFT" to the enum, d in the default branch has type Direction.Left. That is not assignable to never, so the compiler points at the missing case.
Choosing between an enum and a literal union
Enum compatibility rules cause most of the friction. Literal unions follow the ordinary structural rules, so plain strings work.
const Direction = {
Up: "UP",
Down: "DOWN",
} as const;
type Direction = (typeof Direction)[keyof typeof Direction]; // "UP" | "DOWN"
const d: Direction = "UP"; // OK: a literal union accepts the matching string
const e: Direction = Direction.Down; // OK
const f: Direction = "SIDEWAYS"; // Error| Concern | `enum` | `as const` object with a union type |
|---|---|---|
| Assign a matching string literal | Error for string enums | OK |
| Two declarations with equal values | Incompatible | Compatible, because the types are structural |
| Reverse mapping from value to name | Numeric enums only | Not generated |
| Out-of-range `number` variable | Accepted by numeric enums | Rejected |
| Emits runtime code | Yes (except `const enum`) | A plain object |
Choose an enum when you want the nominal protection, so that callers cannot pass an arbitrary string. Choose the union when values arrive from JSON or other external data and you want them to be assignable without conversion.
Exercise
Given enum Level { Low = 1, High = 2 } and declare const n: number;, which of these lines compile in TypeScript 5.x: const a: Level = 2;, const b: Level = 3;, const c: Level = n;? Explain each.
Show answer
a compiles because the literal 2 matches the value of Level.High. b fails because TypeScript 5.0 and later check literal numbers against the enum's member values, and no member has the value 3. c compiles because the type number is assignable to a numeric enum, and the compiler cannot know the runtime value of n.
Exercise
Given enum Color { Red = "RED" }, why does const c: Color = "RED"; fail, and how can you change the declaration so that it compiles?
Show answer
A string enum member has its own identity, so the string literal type "RED" is not assignable to Color. To accept plain strings, replace the enum with a literal union such as type Color = "RED", or with an as const object plus a type derived from its values. Alternatively, keep the enum and write Color.Red at the call site.