Function: mapErr()
Call Signature
mapErr<
T
,E
,F
>(mapErrFn
,result
):Result
<T
,F
>
Map over a Ok
, exactly as in map
, but operating on the value wrapped in an Err
instead of the value wrapped in the Ok
. This is handy for when you need to line up a bunch of different types of errors, or if you need an error of one shape to be in a different shape to use somewhere else in your codebase.
Examples
import { ok, err, mapErr, toString } from 'true-myth/result';
const reason = (err: { code: number, reason: string }) => err.reason;
const anOk = ok(12);
const mappedOk = mapErr(reason, anOk);
console.log(toString(mappedOk)); // Ok(12)
const anErr = err({ code: 101, reason: 'bad file' });
const mappedErr = mapErr(reason, anErr);
console.log(toString(mappedErr)); // Err(bad file)
Type Parameters
T
T
The type of the value wrapped in the Ok
of the Result
.
E
E
The type of the value wrapped in the Err
of the Result
.
F
F
The type of the value wrapped in the Err
of a new Result
, returned by the mapErrFn
.
Parameters
mapErrFn
(e
) => F
The function to apply to the value wrapped in Err
if result
is an Err
.
result
Result
<T
, E
>
The Result
instance to map over an error case for.
Returns
Result
<T
, F
>
Call Signature
mapErr<
T
,E
,F
>(mapErrFn
): (result
) =>Result
<T
,F
>
Map over a Ok
, exactly as in map
, but operating on the value wrapped in an Err
instead of the value wrapped in the Ok
. This is handy for when you need to line up a bunch of different types of errors, or if you need an error of one shape to be in a different shape to use somewhere else in your codebase.
Examples
import { ok, err, mapErr, toString } from 'true-myth/result';
const reason = (err: { code: number, reason: string }) => err.reason;
const anOk = ok(12);
const mappedOk = mapErr(reason, anOk);
console.log(toString(mappedOk)); // Ok(12)
const anErr = err({ code: 101, reason: 'bad file' });
const mappedErr = mapErr(reason, anErr);
console.log(toString(mappedErr)); // Err(bad file)
Type Parameters
T
T
The type of the value wrapped in the Ok
of the Result
.
E
E
The type of the value wrapped in the Err
of the Result
.
F
F
The type of the value wrapped in the Err
of a new Result
, returned by the mapErrFn
.
Parameters
mapErrFn
(e
) => F
The function to apply to the value wrapped in Err
if result
is an Err
.
Returns
(
result
):Result
<T
,F
>
Parameters
result
Result
<T
, E
>
Returns
Result
<T
, F
>