0
0
Typescriptprogramming~20 mins

Literal types and value narrowing in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Literal Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
AOperation succeeded
BType error at compile time
CNo output
DOperation failed
Attempts:
2 left
💡 Hint
Check which literal value is passed to the function and how the if condition narrows the type.
Predict Output
intermediate
2: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";
}
A"vertical"
B"horizontal"
Cundefined
DType error
Attempts:
2 left
💡 Hint
Check which values are considered horizontal and which are vertical.
🔧 Debug
advanced
2: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;
  }
}
ATypeScript compile error: 'perhaps' is not assignable to parameter
BTypeError at runtime
CSyntaxError
DNo error
Attempts:
2 left
💡 Hint
Check the literal values used in the conditions compared to the declared union type.
Predict Output
advanced
2: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"));
A#0000FF
B#FF0000
C#00FF00
Dundefined
Attempts:
2 left
💡 Hint
Check the case that matches the input 'green'.
🧠 Conceptual
expert
2: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";
}
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Consider the initial value and what the if condition changes it to.