capypad
0 day streak
typescript / beginner
Snippet

Numeric Enums

Enums allow you to define a set of named constants. Numeric enums store string names but map them to incrementing numbers starting from a defined value.

snippet.ts
typescript
1
2
3
4
5
6
7
8
enum Direction {
Up = 1,
Down,
Left,
Right
}
 
let move: Direction = Direction.Up;
Breakdown
1
enum Direction {
Starts the definition of a new enumeration called Direction.
2
Up = 1,
Assigns the first constant 'Up' the value 1. Subsequent values will auto-increment.
3
let move: Direction = Direction.Up;
Declares a variable restricted to the values defined in the Direction enum.