Function err

  • Create an instance of Err.

    If you need to create an instance with a specific type (as you do whenever you are not constructing immediately for a function return or as an argument to a function), you can use a type parameter:

    const notString = Result.err<number, string>('something went wrong');
    

    Note: passing nothing, or passing null or undefined explicitly, will produce a Result<T, Unit>, rather than producing the nonsensical and in practice quite annoying Result<null, string> etc. See Unit for more.

    const normalResult = Result.err<number, string>('oh no');
    const explicitUnit = Result.err<number, Unit>(Unit);
    const implicitUnit = Result.err<number, Unit>();

    In the context of an immediate function return, or an arrow function with a single expression value, you do not have to specify the types, so this can be quite convenient.

    type SomeData = {
    //...
    };

    const isValid = (data: SomeData): boolean => {
    // true or false...
    }

    const arrowValidate = (data: SomeData): Result<number, Unit> =>
    isValid(data) ? Result.ok(42) : Result.err();

    function fnValidate(data: someData): Result<number, Unit> {
    return isValid(data) ? Result.ok(42) : Result.err();
    }

    Type Parameters

    • T

    • E

    Returns Result<T, Unit>

    Typeparam

    T The type of the item contained in the Result.

  • Create an instance of Err.

    If you need to create an instance with a specific type (as you do whenever you are not constructing immediately for a function return or as an argument to a function), you can use a type parameter:

    const notString = Result.err<number, string>('something went wrong');
    

    Note: passing nothing, or passing null or undefined explicitly, will produce a Result<T, Unit>, rather than producing the nonsensical and in practice quite annoying Result<null, string> etc. See Unit for more.

    const normalResult = Result.err<number, string>('oh no');
    const explicitUnit = Result.err<number, Unit>(Unit);
    const implicitUnit = Result.err<number, Unit>();

    In the context of an immediate function return, or an arrow function with a single expression value, you do not have to specify the types, so this can be quite convenient.

    type SomeData = {
    //...
    };

    const isValid = (data: SomeData): boolean => {
    // true or false...
    }

    const arrowValidate = (data: SomeData): Result<number, Unit> =>
    isValid(data) ? Result.ok(42) : Result.err();

    function fnValidate(data: someData): Result<number, Unit> {
    return isValid(data) ? Result.ok(42) : Result.err();
    }

    Type Parameters

    • T

    • E

    Parameters

    • error: E

    Returns Result<T, E>

    Typeparam

    T The type of the item contained in the Result.

Generated using TypeDoc