

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
There's a special variant of enums, const enums, which are completely removed during compilation and replaced with their literal values. Unlike regular enums, they don't ship extra mapping code.
const enum Direction {
North = "NORTH",
East = "EAST",
South = "SOUTH",
West = "WEST",
}
const whereWinterComesFrom = Direction.North;
Const enums are more performant, but do come with some limitations:
const enum FavoriteActor {
BradPitt = "Brad Pitt",
AngelinaJolie = "Angelina Jolie",
// this is okay, it references enum members
BestCouple = FavoriteActor.BradPitt + " and " + FavoriteActor.AngelinaJolie,
}
const enum FavoriteActor {
BradPitt = "Brad Pitt",
AngelinaJolie = "Angelina Jolie",
// this is not okay
// const enum member initializers must be constant expressions
BestCouple = getBestCouple(),
}
const enum Direction {
North, // 0
East, // 1
South, // 2
West, // 3
}
const directionValue = Direction.West;
// This errors:
// A const enum member can only be accessed using a string literal.(2476)
const directionName = Direction[directionValue];
// and if you do use a string literal, it just returns the value again
const directionValueAgain = Direction["West"];
// 3
I'd only use const enums when I'm really concerned about performance and bundle size.