Skip to content

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 // 78

An 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:

what tsc emits
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 Level
    setLevel("info")

    So every value from LOG_LEVEL needs a conversion before it can be used.

  • Runtimes that only strip types can’t run enums. Node.js runs .ts files by erasing the types, and erasing doesn’t work for syntax that generates code. The erasableSyntaxOnly flag reports enums, namespaces and constructor parameter properties for that reason. Bun does compile enums, but code you publish may be run elsewhere.

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 codes
exit(3)
  • keyof typeof ExitCode is "Ok" | "Usage" | "Unavailable" | "Config". Indexing the object’s type with all of its keys gives the union of its values. M3 covers keyof properly.
  • The value and the type share a name. TypeScript keeps values and types in separate namespaces, so ExitCode.Config is the object and code: ExitCode is the union.
  • It is plain JavaScript once the types are erased, so it runs anywhere.

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 const
type 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"

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 erasableSyntaxOnly reports them.
  • Prefer const X = { ... } as const with type X = (typeof X)[keyof typeof X].
  • For value-to-name lookups, write the table and check it with satisfies Record<X, ...>.