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.

    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

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

    • E

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

    • F

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

    Parameters

    • mapErrFn: (e: 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>

  • 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.

    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

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

    • E

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

    • F

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

    Parameters

    • mapErrFn: (e: E) => F

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

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