Challenge - 5 Problems
Enum vs Union Literal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of enum vs union literal type usage
What is the output of this TypeScript code when compiled to JavaScript and run?
Typescript
enum Direction {
Up = "UP",
Down = "DOWN"
}
function move(dir: Direction | "LEFT" | "RIGHT") {
switch (dir) {
case Direction.Up:
return "Moving up";
case Direction.Down:
return "Moving down";
case "LEFT":
return "Moving left";
case "RIGHT":
return "Moving right";
}
}
console.log(move(Direction.Up));
console.log(move("LEFT"));Attempts:
2 left
💡 Hint
Check how enum values and union literal strings are compared in the switch.
✗ Incorrect
The enum Direction has string values "UP" and "DOWN". The function move accepts either Direction enum or union literals "LEFT" or "RIGHT". The switch matches the exact string values, so Direction.Up is "UP" which matches case Direction.Up and returns "Moving up". The string "LEFT" matches case "LEFT" and returns "Moving left".
🧠 Conceptual
intermediate1:30remaining
Trade-off: Enum vs Union Literal Types for Type Safety
Which statement best describes a key trade-off between using enums and union literal types in TypeScript?
Attempts:
2 left
💡 Hint
Think about what exists at runtime and how TypeScript narrows types.
✗ Incorrect
Enums compile to real JavaScript objects, so you can iterate over them or access their values at runtime. Union literal types exist only at compile time and provide better type narrowing and simpler syntax but no runtime representation.
🔧 Debug
advanced2:00remaining
Why does this enum code cause a runtime error?
Consider this TypeScript code snippet:
```typescript
enum Status {
Active,
Inactive,
Pending
}
function checkStatus(s: Status) {
if (s === Status.Pending) {
console.log("Pending status");
}
}
checkStatus(3);
```
What is the cause of the runtime error?
Attempts:
2 left
💡 Hint
Check the numeric values assigned to enum members and what happens when you pass 3.
✗ Incorrect
The enum Status assigns Active=0 (default), Inactive=1, Pending=2. Passing 3 is allowed because numeric enums accept any number, but 3 does not correspond to a defined enum member value (Status.Pending is 2), so the comparison fails and it can cause logic errors or undefined behavior if invalid values are not handled.
📝 Syntax
advanced1:30remaining
Which option causes a syntax error in union literal type declaration?
Which of the following union literal type declarations is invalid TypeScript syntax?
Attempts:
2 left
💡 Hint
Check if the literals are quoted strings or identifiers.
✗ Incorrect
Union literal types must be string or number literals (quoted). Option A uses unquoted identifiers (Up, Down, Left, Right) which are not valid string literals and cause a syntax error.
🚀 Application
expert3:00remaining
Choosing between enum and union literal for a feature flag system
You are designing a feature flag system in TypeScript where flags can be "enabled", "disabled", or "experimental". You want to ensure:
- The flags have a runtime representation for logging.
- The code is easy to extend with new flags.
- Type safety is strict to prevent invalid flag values.
Which approach best balances these requirements?
Attempts:
2 left
💡 Hint
Consider runtime representation, type safety, and ease of extension.
✗ Incorrect
Using a plain object with string constants provides a runtime object for logging and iteration. Deriving a union literal type from the object's values ensures strict type safety and easy extension by adding new keys. String enums provide runtime objects but are less flexible to extend and can be verbose. Numeric enums add complexity with mapping. Union literals alone lack runtime representation.