Skip to main content

Function: map()

map<T, U, E>(result, fn): Result<U, E>

Defined in: src/types/result.ts:253

Maps a function over the success value of a Result.

Type Parameters​

• T

Original success value type

• U

Transformed success value type

• E extends Error

Error type

Parameters​

result​

Result<T, E>

Result to map over

fn​

(value) => U

Transformation function

Returns​

Result<U, E>

New Result with transformed value, or original error

Remarks​

If the Result is Ok, applies the function to the value and wraps the result in Ok. If the Result is Err, returns the error unchanged.

Example​

const result = Ok(5);
const doubled = map(result, (x) => x * 2);
// Ok(10)

const errorResult = Err(new Error('Failed'));
const mapped = map(errorResult, (x) => x * 2);
// Err(Error('Failed'))