0
0
Typescriptprogramming~20 mins

Exhaustive checking with never in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Never 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 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 });
ASquare with size 5
BTypeScript compile error due to unreachable default
CCircle with radius 5
DRuntime error because of never assignment
Attempts:
2 left
💡 Hint
Think about what the default case does and when it runs.
Predict Output
intermediate
2: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;
  }
}
AType 'Animal' is not assignable to type 'never' error at _exhaustiveCheck
BNo error, code compiles fine
CRuntime error when calling speak with a cat
DSyntax error due to missing case
Attempts:
2 left
💡 Hint
Check what happens when a union member is not handled in the switch.
🔧 Debug
advanced
2: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?
ABecause the Fruit type is not a discriminated union with a literal 'kind' property
BBecause the default case is unreachable and TypeScript skips the check
CBecause Fruit is not a union type
DBecause the Fruit type is declared with a type alias instead of interface
Attempts:
2 left
💡 Hint
Check the type of the discriminant property and if it is a literal type.
📝 Syntax
advanced
2: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?
A
switch (value) {
  case 'a':
    // do something
    break;
  case 'b':
    // do something else
    break;
  default:
    const check: any = value;
    break;
}
B
switch (value) {
  case 'a':
    // do something
    break;
  default:
    const check: never = value;
    break;
}
C
switch (value) {
  case 'a':
    // do something
  case 'b':
    // do something else
  default:
    const check: never = value;
}
D
switch (value) {
  case 'a':
    // do something
    break;
  case 'b':
    // do something else
    break;
  default:
    const check: never = value;
    break;
}
Attempts:
2 left
💡 Hint
Look for all cases handled and correct use of never in default.
🚀 Application
expert
2: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);
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Count how many shapes have kind 'circle'.