An enum defines a set of named constants. TypeScript has numeric and string enums.
ts
{ , , , }
.;
[];
{
= ,
= ,
}
.;
An enum defines a set of named constants. TypeScript has numeric and string enums.
{ , , , }
.;
[];
{
= ,
= ,
}
.;
Unlike most TypeScript types (which vanish at compile time), a regular enum emits a JavaScript object into your bundle. That's why many teams prefer a union of string literals plus as const, which is purely type-level:
// often preferred — zero runtime cost, easy to read in logs
const Status = { Active: "ACTIVE", Inactive: "INACTIVE" } as const;
type Status = typeof Status[keyof typeof Status]; // "ACTIVE" | "INACTIVE"
// or simply:
type Direction = "up" | "down" | "left" | "right";
const enum E { A, B } // inlined at compile time — no runtime object, but has tooling caveats
Enums give meaningful names to fixed sets (states, roles, directions) and group them under one namespace.
But because they emit runtime code and have quirks (numeric enums are loosely checked), modern TypeScript often favors string-literal unions for simple cases — they're lighter and serialize naturally.
Use enums when you want the namespacing or reverse mapping.