Skip to main content

Overview

Maps a Result<T, E> to Result<U, E> by applying a function to a contained Ok value, leaving an Err value untouched. This is useful for transforming success values through a computation pipeline.

Signature

Parameters

(t: T) => A
required
A function that transforms the Ok value from type T to type A. This function is only called if the Result is Ok.

Returns

Returns a new Result<A, E> where:
  • If the original Result is Ok(value), returns Ok(f(value))
  • If the original Result is Err(error), returns Err(error) unchanged

Examples

Basic Transformation

Skips Errors

Chaining Multiple Maps

Real-World Example

Type Transformations

Implementation Details

From the source code (result.ts:323-325):
For Err (result.ts:431-433):

Notes

  • The mapping function is only executed for Ok values
  • The error type E remains unchanged
  • This method does not catch exceptions thrown by the mapping function
  • For async transformations, use asyncMap()
  • For transformations that return a Result, use andThen()