0
0
Typescriptprogramming~20 mins

Enum vs union literal type trade-offs in Typescript - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum vs Union Literal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"));
A
"Moving up"
"Moving left"
B
"Moving Up"
"Moving Left"
CTypeError at runtime
D
undefined
undefined
Attempts:
2 left
💡 Hint
Check how enum values and union literal strings are compared in the switch.
🧠 Conceptual
intermediate
1: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?
AEnums cannot be used in switch statements, but union literals can.
BEnums provide runtime objects and can be iterated, but union literals offer better type narrowing and simpler code.
CUnion literals create runtime objects, while enums exist only at compile time.
DUnion literals allow adding new values at runtime, enums do not.
Attempts:
2 left
💡 Hint
Think about what exists at runtime and how TypeScript narrows types.
🔧 Debug
advanced
2: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?
AThe function checkStatus is missing a return statement.
BEnums cannot be compared with === operator.
CThe enum values are numeric and 3 is not a valid Status value, causing undefined behavior.
DThe enum Status is not exported, causing a reference error.
Attempts:
2 left
💡 Hint
Check the numeric values assigned to enum members and what happens when you pass 3.
📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in union literal type declaration?
Which of the following union literal type declarations is invalid TypeScript syntax?
Atype Directions = Up | Down | Left | Right;
Btype Colors = "red" | "green" | "blue";
Ctype Sizes = 'small' | 'medium' | 'large';
Dtype Status = "active" | "inactive" | "pending";
Attempts:
2 left
💡 Hint
Check if the literals are quoted strings or identifiers.
🚀 Application
expert
3: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?
AUse a string enum with explicit string values for each flag.
BUse a union literal type of strings without any enum.
CUse numeric enums and map them to strings at runtime.
DUse a plain object with string constants and a union literal type derived from its values.
Attempts:
2 left
💡 Hint
Consider runtime representation, type safety, and ease of extension.