Function: mapErr()
Call Signature
mapErr<
R
,MapperReturn
>(fn
): (result
) =>Result
<UnwrapOk
<R
|MapperReturn
>,InferValueAsErr
<MapperReturn
>>
Defined in: fp.ts:96
Takes a mapper function and returns a function that accepts a Result. The returned function transforms the error of Err results using the mapper, while passing Ok results unchanged.
Type Parameters
R
R
extends Result
The input Result type
MapperReturn
MapperReturn
The type returned by the mapper function
Parameters
fn
(data
) => NonPromise
<MapperReturn
>
Function to transform the Err data
Returns
A function that takes a Result and returns a new Result with transformed error
(
result
):Result
<UnwrapOk
<R
|MapperReturn
>,InferValueAsErr
<MapperReturn
>>
Parameters
result
NonPromise
<R
>
Returns
Result
<UnwrapOk
<R
| MapperReturn
>, InferValueAsErr
<MapperReturn
>>
Example
const result = pipe(
err("network error"),
mapErr((error) => String(error)), // Err<string>
mapErr((error) => ({ error })), // Err<{ error: string }>
);
const okResult = pipe(
ok("success"),
mapErr((error) => String(error)), // Ok<"success"> (unchanged)
mapErr((error) => ({ error })), // Ok<"success"> (unchanged)
);
Call Signature
mapErr<
R
,MapperReturn
>(fn
): (result
) =>Promise
<Result
<UnwrapOk
<R
|MapperReturn
>,InferValueAsErr
<MapperReturn
>>>
Defined in: fp.ts:99
Takes a mapper function and returns a function that accepts a Result. The returned function transforms the error of Err results using the mapper, while passing Ok results unchanged.
Type Parameters
R
R
extends Result
The input Result type
MapperReturn
MapperReturn
The type returned by the mapper function
Parameters
fn
(data
) => OrPromise
<MapperReturn
>
Function to transform the Err data
Returns
A function that takes a Result and returns a new Result with transformed error
(
result
):Promise
<Result
<UnwrapOk
<R
|MapperReturn
>,InferValueAsErr
<MapperReturn
>>>
Parameters
result
OrPromise
<R
>
Returns
Promise
<Result
<UnwrapOk
<R
| MapperReturn
>, InferValueAsErr
<MapperReturn
>>>
Example
const result = pipe(
err("network error"),
mapErr((error) => String(error)), // Err<string>
mapErr((error) => ({ error })), // Err<{ error: string }>
);
const okResult = pipe(
ok("success"),
mapErr((error) => String(error)), // Ok<"success"> (unchanged)
mapErr((error) => ({ error })), // Ok<"success"> (unchanged)
);