0
0
Typescriptprogramming~10 mins

Enum vs union literal type trade-offs in Typescript - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Enum vs union literal type trade-offs
Define Enum
Use Enum value
Compile to JS object
Runtime available
Define Union Literal Type
Use literal values
No JS output
Compile-time only
Shows how enums create runtime objects while union literals exist only at compile time.
Execution Sample
Typescript
enum Color {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE"
}

let c: Color = Color.Red;
Defines an enum Color and assigns a value from it.
Execution Table
StepActionEvaluationResult
1Define enum ColorCreate JS object with keys Red, Green, Blue{ Red: 'RED', Green: 'GREEN', Blue: 'BLUE' }
2Assign c = Color.RedLook up Color.Red'RED'
3Use c in codeValue is 'RED'c = 'RED'
4Define union type ColorUnion = 'RED' | 'GREEN' | 'BLUE'No JS outputType only
5Assign cu: ColorUnion = 'RED'Check if 'RED' is in unionValid assignment
6Use cu in codeValue is 'RED'cu = 'RED'
7EndNo more stepsExecution stops
💡 All assignments valid; union literals have no runtime object; enums do.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
cundefined'RED''RED'N/AN/A'RED'
cuundefinedN/AN/A'RED''RED''RED'
Key Moments - 3 Insights
Why does enum create a JavaScript object but union literal types do not?
Enums generate real JavaScript objects at runtime (see Step 1 in execution_table), so their values exist in the output code. Union literal types are only for compile-time checks and disappear after compilation (see Step 4).
Can you assign any string to a union literal type variable?
No, only the exact strings listed in the union are allowed (see Step 5). Assigning other strings causes a compile error.
Which is safer to use if you want to avoid runtime overhead?
Union literal types have no runtime overhead because they don't create objects (Step 4). Enums create objects which exist at runtime (Step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of variable 'c' after Step 3?
A'Green'
B'RED'
Cundefined
D'BLUE'
💡 Hint
Check the 'Result' column for Step 3 in execution_table.
At which step does the union literal type variable 'cu' get assigned a value?
AStep 2
BStep 3
CStep 5
DStep 1
💡 Hint
Look for 'Assign cu: ColorUnion = 'RED'' in execution_table.
If you want to avoid any runtime JavaScript object creation, which should you choose?
AUnion literal type
BEnum
CBoth create runtime objects
DNeither creates runtime objects
💡 Hint
See the 'Evaluation' and 'Result' columns for Steps 1 and 4 in execution_table.
Concept Snapshot
Enum vs Union Literal Types in TypeScript:
- Enum creates a runtime JS object with named values.
- Union literal types exist only at compile time, no JS output.
- Enums allow reverse mapping and runtime checks.
- Union literals are safer with zero runtime overhead.
- Choose based on need for runtime presence or pure type safety.
Full Transcript
This visual execution compares TypeScript enums and union literal types. Enums create JavaScript objects at runtime, so their values exist in the output code. Union literal types only exist during compilation and disappear after, providing type safety without runtime cost. Variables assigned enum values hold the actual string or number from the enum object. Variables with union literal types hold strings but have no runtime object backing them. This trade-off helps decide when to use enums for runtime features or union literals for lightweight type checks.