October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

TypeScript Enums vs. String Literal Unions: What’s the Difference?

String literal unions restrict values at compile time; regular enums also create a runtime object. Here’s how to choose, including the as const alternative and const enum caveats.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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 Programming Language - Software Engineer & Coder T-Shirt
  • 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

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 const object.
  • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.