Function: mapErr()
Call Signature
mapErr<
R
,MapperReturn
>(result
,mapper
):Result
<UnwrapOk
<R
|MapperReturn
>,InferValueAsErr
<MapperReturn
>>
Defined in: index.ts:657
Transforms the error of an Err result using a mapper function. If the result is Ok, it passes through unchanged. The mapper can return a value, Result, or Promise.
Type Parameters
R
R
extends Result
<any
, any
>
The input Result type
MapperReturn
MapperReturn
The type returned by the mapper function
Parameters
result
NonPromise
<R
>
The Result to map over
mapper
(data
) => NonPromise
<MapperReturn
>
Function to transform the Err data
Returns
Result
<UnwrapOk
<R
| MapperReturn
>, InferValueAsErr
<MapperReturn
>>
A new Result with transformed error, or the original Ok result
Example
mapErr(err('Not found'), (msg) => `Error: ${msg}`); // Result<never, "Error: Not found">
mapErr(ok(42), (msg) => `Error: ${msg}`); // Result<42, never>
mapErr(err(404 as number), code => {
if (code === 404) return ok("Not found, but that's ok");
return err(`HTTP ${code}`);
}); // Result<"Not found, but that's ok", `HTTP ${number}`>
mapErr(err('network failure'), () => ok('default data')); // Result<"default data", never>
Call Signature
mapErr<
R
,MapperReturn
>(result
,mapper
):Promise
<Result
<UnwrapOk
<R
|MapperReturn
>,InferValueAsErr
<MapperReturn
>>>
Defined in: index.ts:661
Transforms the error of an Err result using a mapper function. If the result is Ok, it passes through unchanged. The mapper can return a value, Result, or Promise.
Type Parameters
R
R
extends Result
<any
, any
>
The input Result type
MapperReturn
MapperReturn
The type returned by the mapper function
Parameters
result
OrPromise
<R
>
The Result to map over
mapper
(data
) => OrPromise
<MapperReturn
>
Function to transform the Err data
Returns
Promise
<Result
<UnwrapOk
<R
| MapperReturn
>, InferValueAsErr
<MapperReturn
>>>
A new Result with transformed error, or the original Ok result
Example
mapErr(err('Not found'), (msg) => `Error: ${msg}`); // Result<never, "Error: Not found">
mapErr(ok(42), (msg) => `Error: ${msg}`); // Result<42, never>
mapErr(err(404 as number), code => {
if (code === 404) return ok("Not found, but that's ok");
return err(`HTTP ${code}`);
}); // Result<"Not found, but that's ok", `HTTP ${number}`>
mapErr(err('network failure'), () => ok('default data')); // Result<"default data", never>