

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Enums
incomplete
2: String Enums
incomplete
3: Enum Compilation
incomplete
4: Const Enums
incomplete
5: Enums vs. Union Types
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Enums are a set of defined constants. The simplest form of enum is a numeric enum:
enum Direction {
North, // 0
East, // 1
South, // 2
West, // 3
}
let myDirection: Direction = Direction.North;
console.log(myDirection); // Outputs: 0
The killer feature of enums is that in your code you can have nicely named identifiers like Direction.North, and under the hood you can have simple unique values, like 0. Typescript automatically increments the underlying values for us as we define new enums.
You can also explicitly set the values, and TypeScript will ensure they're unique:
enum StatusCode {
OK = 200,
Created = 201,
BadRequest = 400,
Unauthorized = 401,
NotFound = 404,
}
Numeric enums are bidirectional, which just means you can easily convert from the underlying value to the name and vice versa:
const directionValue: number = Direction.South;
// 2
const directionName: string = Direction[directionValue];
// "South"
Support.ai has an internal system that calculates the severity of customer support requests, so that they can prioritize the most important requests first.
LowMediumHighCriticalLet TypeScript handle setting the unique values by default.