Enums (enumerations) allow developers to define a set of named constants. TypeScript supports numeric enums, string enums, and performance-optimized const enum declarations.
// Numeric Enum
enum Priority {
Low = 1,
Medium, // 2
High, // 3
Critical // 4
}
// String Enum (Recommended for API transparency)
enum OrderStatus {
Pending = "PENDING",
Processing = "PROCESSING",
Shipped = "SHIPPED",
Delivered = "DELIVERED",
Cancelled = "CANCELLED"
}
// Const Enum (Inlined into JS, no runtime object generated)
const enum LogLevel {
Debug = "DEBUG",
Info = "INFO",
Error = "ERROR"
}
interface Order {
id: string;
status: OrderStatus;
priority: Priority;
}
function processOrder(order: Order): void {
console.log(`Order ${order.id} status: ${order.status} | Priority Level: ${order.priority}`);
}
processOrder({
id: "ORD-9921",
status: OrderStatus.Processing,
priority: Priority.High
});
type OrderStatus = 'PENDING' | 'SHIPPED' over enums for lightweight ergonomics.Priority[1] === "Low"), which increases bundle size.Declare a string enum UserRole with values Admin = "ADMIN" and User = "USER". Write a function that accepts UserRole as a parameter.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With