0
0
Typescriptprogramming~10 mins

Exhaustive pattern matching in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exhaustive pattern matching
Start with a value
Match against patterns
Check each pattern
Handle
All cases covered?
Yes
Return matched result
End
The program checks a value against all possible patterns. If all patterns are covered, it returns the matched result. If any pattern is missing, it raises an error.
Execution Sample
Typescript
type Shape = { kind: 'circle', radius: number } | { kind: 'square', size: number };

function area(shape: Shape): number {
  switch (shape.kind) {
    case 'circle': return Math.PI * shape.radius ** 2;
    case 'square': return shape.size ** 2;
  }
}
Calculates area for a shape by matching its kind, covering all possible shape types.
Execution Table
StepInput ValuePattern CheckedMatch ResultAction Taken
1{ kind: 'circle', radius: 3 }case 'circle'MatchCalculate area = π * 3^2 = 28.2743
2{ kind: 'circle', radius: 3 }case 'square'No MatchSkip
3All patterns checkedExhaustivenessAll coveredReturn result 28.2743
4{ kind: 'triangle', base: 4, height: 5 }case 'circle'No MatchSkip
5{ kind: 'triangle', base: 4, height: 5 }case 'square'No MatchSkip
6All patterns checkedExhaustivenessMissing patternError: Non-exhaustive patterns
💡 Execution stops when a matching pattern is found or an error occurs if patterns are not exhaustive.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
shapeundefined{ kind: 'circle', radius: 3 }{ kind: 'circle', radius: 3 }{ kind: 'circle', radius: 3 }{ kind: 'triangle', base: 4, height: 5 }{ kind: 'triangle', base: 4, height: 5 }{ kind: 'triangle', base: 4, height: 5 }
matchedfalsetruefalsefalsefalsefalsefalse
resultundefined28.274328.274328.2743undefinedundefinederror
Key Moments - 2 Insights
Why does the program throw an error if a pattern is missing?
Because exhaustive pattern matching requires all possible cases to be handled. The execution_table rows 4-6 show that when 'triangle' is not matched by any case, the program detects missing patterns and raises an error.
What happens when the first pattern matches?
As shown in execution_table rows 1-3, once a matching pattern is found (case 'circle'), the program calculates the result and stops checking further patterns.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'matched' variable after step 2?
Afalse
Bundefined
Ctrue
Derror
💡 Hint
Check variable_tracker row 'matched' column 'After Step 2' to see if a pattern matched.
At which step does the program detect missing patterns and raise an error?
AStep 4
BStep 6
CStep 3
DStep 2
💡 Hint
Look at execution_table rows 4-6 where the input is a 'triangle' and no pattern matches.
If we add a case for 'triangle', how would the execution_table change for input { kind: 'triangle', base: 4, height: 5 }?
AIt would still raise an error at step 6.
BIt would skip all cases and return undefined.
CIt would match at step 4 and return a result without error.
DIt would match 'circle' case incorrectly.
💡 Hint
Adding a matching case prevents the missing pattern error shown in step 6.
Concept Snapshot
Exhaustive pattern matching checks a value against all possible cases.
All cases must be handled to avoid errors.
Use switch or match expressions with all variants.
If any case is missing, TypeScript signals an error.
This ensures safe and predictable code behavior.
Full Transcript
Exhaustive pattern matching means checking a value against every possible pattern it can have. In TypeScript, this is often done with a switch statement on a discriminated union type. The program tests each case one by one. If a case matches, it runs that code and stops checking further. If no cases match, and some patterns are missing, the program raises an error to warn the developer. This helps catch bugs early by ensuring all possible inputs are handled. The execution table shows how the program checks each pattern step-by-step, and the variable tracker shows how variables change during this process. Key moments clarify why missing patterns cause errors and how matching stops further checks. The visual quiz tests understanding of these steps. The quick snapshot summarizes the main points for easy recall.