Lesson 06 of 55
9 mins readPython Literal Types, String Enums & Const Enums
In Plain English
Literal types restrict values to exact strings, numbers, or booleans (`"GET" | "POST"`). Enums allow declaring a set of named numeric or string constants.
Deep Dive: How It Works
Literal Unions: `type Method = "GET" | "POST" | "PUT" | "DELETE"`.
String Enums: `enum Direction { Up = "UP", Down = "DOWN" }`.
Const Enums: `const enum` completely inlines values during compilation without generating an object in JS output.
Union vs Enum: In modern TypeScript, string literal unions (`type Status = "idle" | "busy"`) are often preferred over enums.
Core Rules to Remember

Literal Unions Preferred: `"light" | "dark"` is lightweight, zero-runtime, and JSON compatible.

Const Enums Inline Values: `const enum` inlines constants directly into call sites for performance.
Live Interactive Example
Hit Run Code to see it liveLiteral Types and Enums
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: Theme allowed only the three exact string values, and LogLevel provided named constants.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Define Action Literal Union
Define `type ActionType = "CREATE" | "UPDATE" | "DELETE";`
Write `function handleAction(act: ActionType): string { return "Action executed: " + act; }`
Log `handleAction("CREATE")`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.