Custom Numeric Enum Values
What you'll learn: How to assign your own numbers to enum members instead of accepting TypeScript's automatic 0, 1, 2... sequence.
Why Custom Values Matter
By default, TypeScript assigns numeric enums starting from 0 and incrementing by 1. But sometimes you need specific numbers—maybe you're matching HTTP status codes, database IDs, or bit flags.
Assigning Explicit Values
You can set any enum member to a specific number. TypeScript will auto-increment subsequent members from that point:
enum HttpStatus {
OK = 200,
Created = 201,
BadRequest = 400,
Unauthorized = 401,
NotFound = 404
}
console.log(HttpStatus.OK); // 200
console.log(HttpStatus.BadRequest); // 400
Partial Custom Assignment
You only need to set the starting point—TypeScript increments from there:
enum Priority {
Low = 1, // explicitly 1
Medium, // auto-increments to 2
High, // auto-increments to 3
Urgent = 10, // explicitly 10
Critical // auto-increments to 11
}
console.log(Priority.Medium); // 2
console.log(Priority.Critical); // 11
Notice how Medium and High followed the auto-increment from Low = 1, but then Critical continued from Urgent = 10.
Real-World Use
This pattern shines when integrating with existing systems that use specific numeric codes, or when you want meaningful numbers (like starting at 1 instead of 0 for human-readable IDs).
Key Takeaway: Assign explicit numeric values to enum members when you need specific numbers; TypeScript auto-increments any members that follow, making it easy to mix custom and automatic numbering.