Overview
Given 2 functions (one for theOk variant and one for the Err variant), execute the function that matches the Result variant. Both functions must return the same type, and the Result is unwrapped to that type.
Signature
Parameters
(t: T) => A
required
A function to execute if the Result is Ok. Receives the success value.
(e: E) => B
required
A function to execute if the Result is Err. Receives the error value.
Returns
ReturnsA | B - the result of whichever function was executed. Unlike map and mapErr, this unwraps the Result into the return type of the callbacks.
Examples
Basic Usage
Side Effects Only
Returning Values
HTTP Response Handling
Converting to Different Types
Forced Error Handling
Equivalent to map + unwrapOr
Real-World Form Validation
Pattern Matching Different Error Types
Implementation Details
From the source code (result.ts:393-395):Err (result.ts:492-494):
Key Differences from map/mapErr
- Unwraps the Result: Returns the callback’s return value directly, not wrapped in a Result
- Forces both cases: You must handle both Ok and Err paths
- Same return type: Both callbacks must return compatible types (A | B)
- Cannot be chained: Since it unwraps, you can’t chain more Result methods after match
Notes
matchis like chainingmapandmapErr, but requires both functions to have the same return type- Both callbacks must be provided - you cannot skip error handling
- The Result is consumed and unwrapped - you cannot chain more Result methods after
- Recommended by
eslint-plugin-neverthrowas a safe way to consume Results - For side effects only, both callbacks can return
void
Related
- Result.map() - Transform Ok values
- Result.mapErr() - Transform Err values
- Result.unwrapOr() - Unwrap with a default value