Use a string literal union when you only need TypeScript to restrict a value to a known set of strings. Use an enum when you also want named members and a runtime object. If you want both runtime constants and a derived union type in ordinary JavaScript object syntax, an as const object is another option.
At a glance: what changes between an enum and a union?
| Question | Enum | String literal union |
|---|---|---|
| Does it exist at runtime? | A regular enum emits a JavaScript object. | No. A type alias is erased and supplies no runtime object. |
| How do you refer to a value? | Through a named member, such as Status.Ready. |
As a string, such as "ready". |
| Does it support reverse lookup? | Numeric enums provide value-to-name reverse mappings; string enums do not. | No. A union alias has no runtime mapping. |
| Can it narrow types? | Enum members participate in enum-member types and enum-union checks. | String literal values work naturally with control-flow and discriminated-union narrowing. |
| When is it useful? | When named members or a runtime object are useful to your code. | When you need only a compile-time constraint on permitted strings. |
How a string literal union works
A union type says that a value can be one of several listed types. For a string literal union, those types are exact strings:
As an Amazon Associate I earn from qualifying purchases.
type Status = "ready" | "loading" | "failed";
function setStatus(status: Status) {
// ...
}
setStatus("ready"); // allowed
setStatus("pending"); // type error
The type helps catch misspellings and other values outside the set. At runtime, though, Status does not exist: it is a type alias, not a JavaScript value. The strings remain ordinary strings.
Recommended Free Tools
Literal strings also work well in control flow. When a property acts as a discriminant, checking its literal value can narrow a union to the corresponding case:
#1 Best Overall
type Result =
| { status: "ready"; data: string }
| { status: "failed"; error: Error };
function describe(result: Result) {
if (result.status === "ready") {
return result.data;
}
return result.error.message;
}
What an enum adds at runtime
A regular enum creates an object as well as a type-level construct:
enum Status {
Ready = "ready",
Loading = "loading",
Failed = "failed",
}
const current = Status.Ready;
The member names provide a consistent way to refer to the values, and the enum object can be passed to runtime code or inspected. String enum values are readable strings, which can be useful when values are serialized. String enum members must be initialized with string literals or other string enum members; they do not have numeric-enum auto-increment behavior.
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
Numeric enums have a distinct runtime behavior: their emitted objects include reverse mappings from numeric values to member names. String enums do not. Do not assume that a string enum provides a reverse lookup such as Status["ready"].
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Choose based on whether you need a runtime value
Choose a string literal union for a type-only constraint
If values are already represented as strings and your goal is to limit which strings callers can use, a union is usually the direct fit. It keeps the value representation in plain JavaScript strings and adds no enum object. It is also a natural choice for discriminants used to narrow related object shapes.
Choose an enum for named members and an enum object
Use a regular enum when code benefits from references such as Status.Ready or needs the enum object at runtime. The trade-off is that regular enum syntax emits runtime JavaScript rather than serving only as a type declaration.
Use an as const object for runtime constants plus a derived type
An object marked as const can retain runtime values while TypeScript derives a union from them:
const Status = {
Ready: "ready",
Loading: "loading",
Failed: "failed",
} as const;
type Status = typeof Status[keyof typeof Status];
This provides named runtime properties and a type containing their literal values, using ordinary object syntax. The cost is the type-extraction expression needed to derive the union.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Where const enums fit—and their caveat
A const enum is a separate option from both a regular enum and a string literal union. Its members are inlined and the enum is erased rather than emitted as a regular runtime object. That may appear to offer enum-style member references without the same runtime object, but library authors need to account for compiler and consumer compatibility.
Best Value
TypeScript documents risks for ambient const enums when a library and downstream project compile against different versions, and when consumers use isolatedModules. In those contexts, inlined values can create version-skew problems or compilation incompatibilities. Do not treat const enums as an interchangeable choice for every application or library toolchain.
Quick Recap
Practical decision
- Need to reject strings outside a known set, with no runtime list? Use a string literal union.
- Need named members and a runtime object? Use a regular enum.
- Need named runtime constants and a type derived from their values, in object syntax? Consider an
as constobject. - Considering a const enum for a library? Check the documented ambient-const-enum risks against your consumers’ compiler settings and versioning.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




