Enums vs as const objects
When Relay exits, the exit code tells the supervisor (systemd, podman, a CI runner) what went
wrong. Relay borrows the codes from the BSD sysexits.h convention: 64 for bad usage, 69 for an
unavailable service, 78 for a configuration error. A restart policy can then retry on 69 and
give up on 78. Those codes need names, and TypeScript offers two ways to give them.
enum ExitCode { Ok = 0, Usage = 64, Unavailable = 69, Config = 78}
const code = ExitCode.Config // 78An enum is one of the few TypeScript features that is not just types on top of JavaScript. It generates code. A numeric enum compiles to an object with a reverse mapping from value to name:
var ExitCode;(function (ExitCode) { ExitCode[ExitCode["Ok"] = 0] = "Ok"; ExitCode[ExitCode["Usage"] = 64] = "Usage"; // ...})(ExitCode || (ExitCode = {}));// Object.keys(ExitCode): ["0", "64", "69", "78", "Ok", "Usage", "Unavailable", "Config"]Three consequences:
-
The object has twice the keys you wrote. Iterating over it gives names and numbers mixed.
-
String enums aren’t interchangeable with their strings. A function that takes the enum rejects the plain value it stands for:
enum Level {Info = "info",Warn = "warn"}function setLevel(level: Level) {return level}setLevel(Level.Info)// @ts-expect-error: "info" is not assignable to LevelsetLevel("info")So every value from
LOG_LEVELneeds a conversion before it can be used. -
Runtimes that only strip types can’t run enums. Node.js runs
.tsfiles by erasing the types, and erasing doesn’t work for syntax that generates code. TheerasableSyntaxOnlyflag reports enums, namespaces and constructor parameter properties for that reason. Bun does compile enums, but code you publish may be run elsewhere.
as const objects
Section titled “as const objects”A plain object with as const gives the names, and one line derives the union of its values:
const ExitCode = { Ok: 0, Usage: 64, Unavailable: 69, Config: 78} as const
type ExitCode = (typeof ExitCode)[keyof typeof ExitCode] // 0 | 64 | 69 | 78
function exit(code: ExitCode) { return code}
exit(ExitCode.Config)exit(78) // the plain value is accepted too// @ts-expect-error: 3 is not one of the codesexit(3)keyof typeof ExitCodeis"Ok" | "Usage" | "Unavailable" | "Config". Indexing the object’s type with all of its keys gives the union of its values. M3 coverskeyofproperly.- The value and the type share a name. TypeScript keeps values and types in separate namespaces,
so
ExitCode.Configis the object andcode: ExitCodeis the union. - It is plain JavaScript once the types are erased, so it runs anywhere.
Going from value to name
Section titled “Going from value to name”An enum’s reverse mapping is the one thing you lose. Write the mapping out, and let satisfies
check that it covers every code:
const ExitCode = { Ok: 0, Usage: 64, Unavailable: 69, Config: 78 } as consttype ExitCode = (typeof ExitCode)[keyof typeof ExitCode]
const NAMES = { 0: "Ok", 64: "Usage", 69: "Unavailable", 78: "Config" } as const satisfies Record< ExitCode, keyof typeof ExitCode>
const name = NAMES[78] // "Config"Exercise
Section titled “Exercise”Replace the enum with an as const object and a derived type (the hidden tests require
ExitCode to be exactly 0 | 64 | 69 | 78). Fix the wrong mapping the tests find, and write a
nameOf that doesn’t rely on a reverse mapping.
- Enums generate code: numeric enums carry a reverse mapping, and string enums reject their own plain values.
- Type-stripping runtimes can’t run them, and
erasableSyntaxOnlyreports them. - Prefer
const X = { ... } as constwithtype X = (typeof X)[keyof typeof X]. - For value-to-name lookups, write the table and check it with
satisfies Record<X, ...>.
Further reading
Section titled “Further reading”- Handbook: Enums: how enums compile and their pitfalls.
- Node.js docs: Modules: TypeScript: why type stripping can’t run enums.
- TSConfig Reference: the
erasableSyntaxOnlyoption.