Challenge - 5 Problems
Literal Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code using literal types?
Consider this TypeScript code snippet that uses literal types and narrowing. What will be logged to the console?
Typescript
function printStatus(status: "success" | "error") { if (status === "success") { console.log("Operation succeeded"); } else { console.log("Operation failed"); } } printStatus("error");
Attempts:
2 left
💡 Hint
Check which literal value is passed to the function and how the if condition narrows the type.
✗ Incorrect
The function accepts only two literal strings: 'success' or 'error'. When called with 'error', the else branch runs, logging 'Operation failed'.
❓ Predict Output
intermediate2:00remaining
What is the value of variable 'result' after narrowing?
Given this TypeScript code, what is the value of 'result' after the if statement?
Typescript
type Direction = "left" | "right" | "up" | "down"; let move: Direction = "up"; let result: string; if (move === "left" || move === "right") { result = "horizontal"; } else { result = "vertical"; }
Attempts:
2 left
💡 Hint
Check which values are considered horizontal and which are vertical.
✗ Incorrect
Since 'move' is 'up', it matches the else branch, so 'result' is assigned 'vertical'.
🔧 Debug
advanced2:00remaining
What error does this TypeScript code raise?
This code tries to narrow a union of literal types but causes an error. What error is raised?
Typescript
function handleInput(input: "yes" | "no" | "maybe") { if (input === "yes") { return true; } else if (input === "no") { return false; } else if (input === "perhaps") { return null; } }
Attempts:
2 left
💡 Hint
Check the literal values used in the conditions compared to the declared union type.
✗ Incorrect
The union type does not include 'perhaps', so checking for it causes a compile-time error.
❓ Predict Output
advanced2:00remaining
What is the output of this function using literal type narrowing with switch?
Analyze this TypeScript function using a switch statement with literal types. What will it print when called with 'green'?
Typescript
function getColorCode(color: "red" | "green" | "blue") { switch (color) { case "red": return "#FF0000"; case "green": return "#00FF00"; case "blue": return "#0000FF"; } } console.log(getColorCode("green"));
Attempts:
2 left
💡 Hint
Check the case that matches the input 'green'.
✗ Incorrect
The switch matches 'green' and returns '#00FF00', which is then logged.
🧠 Conceptual
expert2:00remaining
How many possible values can variable 'status' have after narrowing?
Given this TypeScript code, after the if statement, how many possible literal values can 'status' have?
Typescript
type Status = "pending" | "approved" | "rejected"; let status: Status = "pending"; if (status !== "pending") { status = "approved"; }
Attempts:
2 left
💡 Hint
Consider the initial value and what the if condition changes it to.
✗ Incorrect
Initially, 'status' is 'pending'. If it is not 'pending', it is set to 'approved'. So after the if, 'status' can be either 'pending' or 'approved'.