capypad
0 day streak
typescript / beginner
Snippet

String Enums

String enums allow you to define a set of named constants using string values. This makes the code more readable during debugging as the values have clear names.

snippet.ts
typescript
1
2
3
4
5
6
7
8
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
 
const currentMove = Direction.Up;
Breakdown
1
enum Direction
Declares a new enumeration named Direction.
2
Up = "UP"
Assigns the string value 'UP' to the member 'Up'.
3
Direction.Up
Accesses the enum member in a type-safe manner.