

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
Numeric enums can be nice when:
But often, string enums are easier to work with if you just want labels.
enum LogLevel {
ERROR = "ERROR",
WARN = "WARN",
INFO = "INFO",
DEBUG = "DEBUG",
}
function structuredLog(message: string, level: LogLevel) {
console.log(`[${level}] ${message}`);
}
structuredLog("User not found", LogLevel.ERROR);
// Outputs: [ERROR] User not found
When enums only exist within your code, numeric enums are totally fine. They start to get really hairy when you need to serialize them to JSON or store them in a database. There's nothing worse than debugging API responses and seeing this:
{
"id": "94e83b65-ae9c-47f4-b788-d3f4fd085067",
"name": "Lane",
"user_type": 7 // what the h*ck is 7?!?!?
}
It's hard to tell the severity of a request record in the Support.ai database, because it's just a number. Let's convert them to more readable strings.
RequestSeverity string value.When an enum is not given a value, it starts at 0 and auto-increments from there.