Skip to main content

Function: mapErr()

mapErr<T, E1, E2>(result, fn): Result<T, E2>

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

Maps a function over the error of a Result.

Type Parameters

T

Success value type

E1 extends Error

Original error type

E2 extends Error

Transformed error type

Parameters

result

Result<T, E1>

Result to map over

fn

(error) => E2

Error transformation function

Returns

Result<T, E2>

New Result with transformed error, or original value

Remarks

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

Useful for wrapping errors in more specific error types or adding context.

Example

const result = Err(new Error('Failed'));
const wrapped = mapErr(result, (err) => new ValidationError(err.message));
// Err(ValidationError('Failed'))