Function mapErr

  • 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

    • E

    • F

    Parameters

    • mapErrFn: ((e) => F)

      The function to apply to the value wrapped in Err if result is an Err.

        • (e): F
        • Parameters

          • e: E

          Returns F

    • result: Result<T, E>

      The Result instance to map over an error case for.

    Returns Result<T, F>

    Typeparam

    T The type of the value wrapped in the Ok of the Result.

    Typeparam

    E The type of the value wrapped in the Err of the Result.

    Typeparam

    F The type of the value wrapped in the Err of a new Result, returned by the mapErrFn.

  • Type Parameters

    • T

    • E

    • F

    Parameters

    • mapErrFn: ((e) => F)
        • (e): F
        • Parameters

          • e: E

          Returns F

    Returns ((result) => Result<T, F>)

Generated using TypeDoc