TypeScript — Enums
Basic enum
enum Direction {
Up,
Down,
Left,
Right
}
let dir: Direction = Direction.Up;
String enum
enum Color {
Red = 'RED',
Green = 'GREEN',
Blue = 'BLUE'
}
let color: Color = Color.Red;
Const enum
const enum Status {
Active = 'ACTIVE',
Inactive = 'INACTIVE'
}
let status: Status = Status.Active;
Reverse mapping
enum Direction {
Up,
Down,
Left,
Right
}
console.log(Direction[0]); // "Up"
Computed members
enum FileAccess {
None,
Read = 1 << 1,
Write = 1 << 2,
ReadWrite = Read | Write
}
Mini Practice
- Create basic enum
- Use string enum
- Create const enum
- Use reverse mapping
Up Next
Continue with Interfaces - Interface definitions.
Related Topics
Frequently Asked Questions about Enums
What is Enums in TypeScript?
Enums is a fundamental concept in TypeScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Enums?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Enums.
Why is Enums important in TypeScript?
Enums is essential for TypeScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.