TypeScript Enums
Enums (enumerations) allow you to define a set of named constants. They make code more readable and maintainable by giving meaningful names to numeric or string values.
Numeric Enums
Numeric enums auto-increment from 0 by default:
Output
Click Run to execute your code
String Enums
String enums use string values instead of numbers:
Output
Click Run to execute your code
Const Enums
Const enums are completely removed during compilation for better performance:
Output
Click Run to execute your code
Exercise: Status System
Task: Create an order status system using enums!
Output
Click Run to execute your code
Summary
- Numeric enums auto-increment from 0
- String enums use explicit string values
- Const enums are inlined at compile time
- Enums improve code readability
What's Next?
Next, we'll explore Special Types like any, unknown, void, and never!
Enjoying these tutorials?