0
0
Typescriptprogramming~10 mins

Exhaustive checking with never in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exhaustive checking with never
Start with union type
Switch on union cases
Handle each case explicitly
Use 'never' for unhandled cases
Compiler error if any case missed
End
This flow shows how to check all cases of a union type using a switch, and use 'never' to catch missing cases at compile time.
Execution Sample
Typescript
type Shape = 'circle' | 'square';
function area(shape: Shape) {
  switch(shape) {
    case 'circle': return 3.14 * 1 * 1;
    case 'square': return 1 * 1;
    default: const _exhaustiveCheck: never = shape;
  }
}
This code calculates area for shapes and uses 'never' in default to ensure all cases are handled.
Execution Table
StepInput shapeSwitch case matchedActionResult / Error
1'circle'case 'circle'Calculate area as 3.14 * 1 * 13.14
2'square'case 'square'Calculate area as 1 * 11
3'triangle'defaultAssign shape to never variableType error: 'triangle' not assignable to never
💡 Execution stops because 'triangle' is not part of Shape union, causing compile-time error at default case.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
shapeundefined'circle''square''triangle'
_exhaustiveCheckundefinedN/AN/AError: type mismatch
Key Moments - 2 Insights
Why does assigning 'shape' to a variable of type 'never' cause an error in the default case?
Because 'never' means no value is allowed. If the default case runs, it means some case was missed. The compiler uses this to catch unhandled cases, as shown in execution_table row 3.
What happens if we add a new shape to the union but forget to handle it in the switch?
The default case will catch it by assigning to 'never', causing a compile-time error. This forces you to handle the new shape explicitly, as seen in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result when input shape is 'circle'?
AType error
B1
C3.14
Dundefined
💡 Hint
Check execution_table row 1 under 'Result / Error'
At which step does the compiler error occur due to an unhandled case?
AStep 3
BStep 1
CStep 2
DNo error
💡 Hint
See execution_table row 3 where default case triggers error
If we remove the default case, what happens when a new shape is added but not handled?
ACompiler error still occurs
BNo error, but runtime may be wrong
CCode runs correctly
DTypeScript warns but compiles
💡 Hint
Default case with 'never' enforces compile-time check; without it, missing cases are not caught (see key_moments)
Concept Snapshot
Exhaustive checking with 'never':
- Use a switch on union types
- Handle all cases explicitly
- Add a default case assigning to a 'never' variable
- Compiler errors if any case is missed
- Helps catch bugs early by ensuring all cases handled
Full Transcript
This example shows how to use TypeScript's 'never' type to ensure all cases of a union type are handled in a switch statement. The code defines a union type 'Shape' with 'circle' and 'square'. The function 'area' switches on the shape and returns the area for each case. The default case assigns the shape to a variable of type 'never'. Since 'never' means no value is possible, if the default case runs, the compiler throws an error. This helps catch missing cases at compile time. The execution table shows the steps for inputs 'circle', 'square', and an invalid 'triangle'. The variable tracker shows how 'shape' changes and how '_exhaustiveCheck' causes an error when assigned an unexpected value. Key moments clarify why 'never' causes errors and how it helps maintain code safety. The quiz tests understanding of results, error steps, and the role of the default case. The snapshot summarizes the pattern for quick reference.