exhaustive
exhaustive() unwraps a PatternResult and checks that all cases have been covered. It is used both with the builder match(...).exhaustive() and in a direct call on any PatternResult. If a pattern is missing, an InvalidExhaustivePatternError is thrown at call time.
Interactive example
Syntax
typescript
function exhaustive<
const GenericInput extends unknown,
GenericResult extends PatternResult<GenericInput>
>(result: GenericResult): Unwrap<GenericResult>;Parameters
result: aPatternResultreturned by amatchbuilder (after.with()/.when()).
Return value
The value actually contained in the PatternResult, with the type refined. If the initial union has not been fully covered, the call fails and shows you the remaining value.
Best practices
- On the builder:
match(...).with(...).exhaustive()ensures each branch is covered before returning the final value. - In direct call: pass any
PatternResultto unwrap it and force the exhaustiveness check. - During development, let
exhaustivepoint out missing cases rather than adding an overly broadotherwise.
