Recall & Review
beginner
What is exhaustive pattern matching in TypeScript?
Exhaustive pattern matching means checking all possible cases of a value so that no case is missed. It helps catch errors when new cases are added.
Click to reveal answer
intermediate
How can you enforce exhaustive checks in TypeScript's
switch statements?You can add a
default case that throws an error or uses the never type to make sure all cases are handled.Click to reveal answer
intermediate
What is the
never type used for in exhaustive pattern matching?The
never type represents values that should never happen. Using it in a default case helps TypeScript warn if some cases are missing.Click to reveal answer
beginner
Why is exhaustive pattern matching helpful in real-life programming?
It prevents bugs by making sure you handle every possible case, especially when your data types change or grow over time.
Click to reveal answer
intermediate
Show a simple example of exhaustive pattern matching with a
switch on a union type in TypeScript.```typescript
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; size: number };
function area(shape: Shape) {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2;
case 'square':
return shape.size ** 2;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}
```Click to reveal answer
What does the
never type represent in TypeScript?✗ Incorrect
The
never type means a value that should never happen, useful for exhaustive checks.Why add a
default case with a never type in a switch?✗ Incorrect
Adding a
default with never helps TypeScript warn if some cases are not handled.What happens if you forget a case in exhaustive pattern matching?
✗ Incorrect
If you use exhaustive checks, TypeScript warns you about missing cases to avoid bugs.
Which TypeScript feature helps you create union types for pattern matching?
✗ Incorrect
Union types with | let you combine types for pattern matching.
What is the main benefit of exhaustive pattern matching?
✗ Incorrect
Exhaustive matching ensures all cases are handled, preventing bugs.
Explain how to implement exhaustive pattern matching in TypeScript using a switch statement.
Think about how to make TypeScript check all cases.
You got /4 concepts.
Why is exhaustive pattern matching important when your data types change over time?
Consider what happens if you add a new case but forget to handle it.
You got /3 concepts.