Overview
Maps aResult<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
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 newResult<A, E> where:
- If the original Result is
Ok(value), returnsOk(f(value)) - If the original Result is
Err(error), returnsErr(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):Err (result.ts:431-433):
Notes
- The mapping function is only executed for
Okvalues - The error type
Eremains 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()
Related
- Result.mapErr() - Transform error values
- Result.andThen() - Chain computations that return Results
- Result.asyncMap() - Map with async functions