Function unwrapOr

  • Safely get the value out of the Ok variant of a Result.

    This is the recommended way to get a value out of a Result most of the time.

    import { ok, err, unwrapOr } from 'true-myth/result';

    const anOk = ok<number, string>(12);
    console.log(unwrapOr(0, anOk)); // 12

    const anErr = err<number, string>('nooooo');
    console.log(unwrapOr(0, anErr)); // 0

    Type Parameters

    • T

      The value wrapped in the Ok.

    • U
    • E

      The value wrapped in the Err.

    Parameters

    • defaultValue: U

      The value to use if result is an Err.

    • result: Result<T, E>

      The Result instance to unwrap if it is an Ok.

    Returns U | T

    The content of result if it is an Ok, otherwise defaultValue.

  • Safely get the value out of the Ok variant of a Result.

    This is the recommended way to get a value out of a Result most of the time.

    import { ok, err, unwrapOr } from 'true-myth/result';

    const anOk = ok<number, string>(12);
    console.log(unwrapOr(0, anOk)); // 12

    const anErr = err<number, string>('nooooo');
    console.log(unwrapOr(0, anErr)); // 0

    Type Parameters

    • T

      The value wrapped in the Ok.

    • U
    • E

      The value wrapped in the Err.

    Parameters

    • defaultValue: U

      The value to use if result is an Err.

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

    The content of result if it is an Ok, otherwise defaultValue.