Basic Numeric Enum Syntax
What you'll learn: How to define TypeScript's most basic enum and understand its automatic numbering behavior.
What Is a Numeric Enum?
A numeric enum is TypeScript's built-in way to create a set of named constants that map to numeric values. Unlike the literal unions you've been working with, enums give you a dedicated syntax for defining a collection of related values — and TypeScript automatically assigns numbers to each member.
Think of it like a numbered list where you give meaningful names to positions. Instead of remembering that status 0 means "pending" and status 1 means "approved," you can use clear names that TypeScript converts to numbers behind the scenes.
The Syntax
Here's the simplest numeric enum:
enum Status {
Pending,
Approved,
Rejected
}
TypeScript automatically assigns:
Status.Pending→0Status.Approved→1Status.Rejected→2
You can use these members just like any other value:
let currentStatus: Status = Status.Pending;
if (currentStatus === Status.Approved) {
console.log("Access granted");
}
Notice how the auto-increment starts at 0 by default and increases by 1 for each subsequent member — just like array indices.
Why This Matters
Numeric enums give you type-safe named constants without manually creating literal unions. The enum becomes both a type and a collection of values you can reference, all with autocomplete support.
Key Takeaway: Numeric enums use the enum keyword to create auto-numbered constants starting from 0, giving you readable names backed by sequential numeric values.