Enums & Literal TypesBeginner
1Question 1 of 1
typescriptWhat is the difference between numeric Enums, string Enums, and Literal Types?
Explanation & Concept
• Numeric Enums: Assign auto-incrementing numbers (0, 1, 2) and support reverse mapping. However, they can allow out-of-bounds numbers. • String Enums: Bind keys to explicit string values with strong readability. • String Literal Unions (`type Direction = 'north' | 'south'`): Lightweight, zero runtime JavaScript bundle overhead, and widely preferred in modern TypeScript.
Code Example
typescript
// String Literal Union (Zero JS runtime footprint)
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
// String Enum (Generates a runtime JavaScript object)
enum UserRole {
Admin = "ADMIN",
Editor = "EDITOR",
Viewer = "VIEWER"
}
function sendRequest(url: string, method: HttpMethod, role: UserRole) {
console.log(`Sending ${method} to ${url} for ${role}`);
}How It Works & Key Takeaways
Literal unions disappear completely during compilation into JavaScript, resulting in smaller bundles while preserving strict type safety.
Mock Test · 1 Questions · Optional
Ready for Mapped & Conditional Types?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions