Skip to main content

Overview

Given 2 functions (one for the Ok 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

Returns A | 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):
For Err (result.ts:492-494):

Key Differences from map/mapErr

  1. Unwraps the Result: Returns the callback’s return value directly, not wrapped in a Result
  2. Forces both cases: You must handle both Ok and Err paths
  3. Same return type: Both callbacks must return compatible types (A | B)
  4. Cannot be chained: Since it unwraps, you can’t chain more Result methods after match

Notes

  • match is like chaining map and mapErr, 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-neverthrow as a safe way to consume Results
  • For side effects only, both callbacks can return void