Challenge - 5 Problems
Never 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 exhaustive check with never?
Consider this TypeScript code that uses exhaustive checking with
never. What will be logged when handleShape is called with a Circle?Typescript
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; size: number };
function handleShape(shape: Shape) {
switch (shape.kind) {
case 'circle':
console.log(`Circle with radius ${shape.radius}`);
break;
case 'square':
console.log(`Square with size ${shape.size}`);
break;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}
handleShape({ kind: 'circle', radius: 5 });Attempts:
2 left
💡 Hint
Think about what the
default case does and when it runs.✗ Incorrect
The function handles all possible
kind values of Shape. The default case is never reached at runtime because all cases are covered. The never assignment helps TypeScript check exhaustiveness but does not affect runtime output.❓ Predict Output
intermediate2:00remaining
What error does this exhaustive check produce?
Given this TypeScript code, what error will the compiler produce?
Typescript
type Animal = { type: 'dog'; bark: () => void } | { type: 'cat'; meow: () => void };
function speak(animal: Animal) {
switch (animal.type) {
case 'dog':
animal.bark();
break;
// Missing case for 'cat'
default:
const _exhaustiveCheck: never = animal;
return _exhaustiveCheck;
}
}Attempts:
2 left
💡 Hint
Check what happens when a union member is not handled in the switch.
✗ Incorrect
Because the 'cat' case is missing, the default case receives an Animal type that is not 'never'. Assigning it to a variable typed 'never' causes a TypeScript compile error.
🔧 Debug
advanced2:30remaining
Why does this exhaustive check fail to catch a missing case?
Examine this TypeScript code. Why does the exhaustive check with
never not produce a compile error even though a case is missing?Typescript
type Fruit = { kind: 'apple'; color: string } | { kind: 'banana'; length: number } | { kind: 'orange'; diameter: number };
function describeFruit(fruit: Fruit) {
switch (fruit.kind) {
case 'apple':
console.log(`Apple color: ${fruit.color}`);
break;
case 'banana':
console.log(`Banana length: ${fruit.length}`);
break;
// Missing 'orange' case
default:
const _exhaustiveCheck: never = fruit;
return _exhaustiveCheck;
}
}
// Why no compile error?Attempts:
2 left
💡 Hint
Check the type of the discriminant property and if it is a literal type.
✗ Incorrect
Exhaustive checking with never works only if the union is discriminated by a literal type property. If the 'kind' property is not a literal type but a general string, TypeScript cannot narrow the type and the check fails silently.
📝 Syntax
advanced2:00remaining
Which option correctly uses exhaustive checking with never?
Which of the following TypeScript code snippets correctly implements exhaustive checking with
never in a switch statement?Attempts:
2 left
💡 Hint
Look for all cases handled and correct use of never in default.
✗ Incorrect
Option D handles all cases with breaks and uses a variable typed never in default to enforce exhaustiveness. Option D misses a case, C misses breaks causing fallthrough, D uses any instead of never.
🚀 Application
expert2:30remaining
How many items are in the resulting array after exhaustive check filtering?
Given this TypeScript code that uses exhaustive checking with never and filters an array of shapes, how many items remain in
filteredShapes?Typescript
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; size: number } | { kind: 'triangle'; base: number; height: number };
const shapes: Shape[] = [
{ kind: 'circle', radius: 10 },
{ kind: 'square', size: 5 },
{ kind: 'triangle', base: 4, height: 3 }
];
function isCircle(shape: Shape): shape is Extract<Shape, { kind: 'circle' }> {
switch (shape.kind) {
case 'circle':
return true;
case 'square':
case 'triangle':
return false;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}
const filteredShapes = shapes.filter(isCircle);Attempts:
2 left
💡 Hint
Count how many shapes have kind 'circle'.
✗ Incorrect
The filter keeps only shapes where isCircle returns true. Only one shape has kind 'circle', so filteredShapes has length 1.