TypeScript 7 reference · Type Operators and Keywords

TypeScript enum Keyword

Define a set of named constants. Prefer union types or 'as const' objects in new code; use enums when you need a runtime object.

TypeScript 7 Since 1.0

Create a free account to save this page, take notes and track your progress. Sign in

Examples

A string enumTry it yourself
enum Status {
  Draft = "draft",
  Published = "published",
}
function label(s: Status) {
  return s === Status.Published ? "Live" : "Not live";
}
console.log(label(Status.Published), Object.values(Status));

Output

Live [ 'draft', 'published' ]
The 'as const' alternativeTry it yourself
const Status = { Draft: "draft", Published: "published" } as const;
type Status = (typeof Status)[keyof typeof Status];
const s: Status = Status.Draft;
console.log(s);

Output

draft

Numeric enums allow reverse lookups and accept any number, which hides bugs; string enums or as const objects are safer. TypeScript's erasableSyntaxOnly option (and Node's built-in type stripping) disallow enums because they generate runtime code.

Version history

VersionChange
TypeScript 1.0Added

Was this page helpful?