0
0
Typescriptprogramming~20 mins

Exhaustive pattern matching in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Exhaustive Matcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of exhaustive match with union types
What is the output of this TypeScript code using exhaustive pattern matching with match?
Typescript
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; size: number };

function area(shape: Shape): number {
  return match (shape.kind) {
    case 'circle':
      return Math.PI * shape.radius ** 2;
    case 'square':
      return shape.size ** 2;
  }
}

console.log(area({ kind: 'circle', radius: 3 }).toFixed(2));
A"28.27"
B"9"
CTypeScript compile error due to missing case
D"18.85"
Attempts:
2 left
πŸ’‘ Hint
Think about the formula for the area of a circle and how the code calculates it.
❓ Predict Output
intermediate
2:00remaining
Result of exhaustive match with never type
What will this TypeScript code output when run?
Typescript
type Status = 'success' | 'error';

function handleStatus(status: Status) {
  return match (status) {
    case 'success':
      return 'Yay!';
    case 'error':
      return 'Oops!';
  }
}

console.log(handleStatus('error'));
A"Yay!"
BTypeScript compile error: Non-exhaustive match
CRuntime error: Unhandled case
D"Oops!"
Attempts:
2 left
πŸ’‘ Hint
Check which case matches the input 'error'.
πŸ”§ Debug
advanced
2:30remaining
Identify the error in exhaustive match
What error does this TypeScript code produce?
Typescript
type Command = { type: 'start' } | { type: 'stop' } | { type: 'pause' };

function execute(cmd: Command) {
  return match (cmd.type) {
    case 'start':
      return 'Started';
    case 'stop':
      return 'Stopped';
  }
}

execute({ type: 'pause' });
AReturns undefined for 'pause' case
BTypeScript compile error: Non-exhaustive match, missing 'pause' case
CRuntime error: Unhandled case 'pause'
DNo error, returns 'Stopped'
Attempts:
2 left
πŸ’‘ Hint
Check if all union cases are handled in the match.
πŸ“ Syntax
advanced
2:00remaining
Which option causes a syntax error in exhaustive match?
Which of the following TypeScript code snippets causes a syntax error due to incorrect exhaustive pattern matching syntax?
Areturn match (value) { case 'a': 1 case 'b': 2 }
Breturn match (value) { case 'a': return 1; case 'b': return 2; }
Creturn match (value) { case 'a': 1; case 'b': 2; default: 0; }
Dreturn match (value) { case 'a': 1; case 'b': 2; }
Attempts:
2 left
πŸ’‘ Hint
Look for missing punctuation or keywords in the match cases.
πŸš€ Application
expert
2:00remaining
Number of cases needed for exhaustive match
Given this TypeScript union type, how many cases must a match statement cover to be exhaustive?
Typescript
type Event =
  | { type: 'click'; x: number; y: number }
  | { type: 'keydown'; key: string }
  | { type: 'resize'; width: number; height: number }
  | { type: 'scroll'; scrollTop: number };

// How many cases are needed in match(event.type) to be exhaustive?
A3
B5
C4
DDepends on runtime values
Attempts:
2 left
πŸ’‘ Hint
Count the distinct union members by their 'type' property.