• 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 = err<number, string>('something went wrong');
    

    Note: passing nothing will produce a Result<T, void>, passing undefined will produce a Result<T, undefined> which is compatible with Result<T, void>.

    const normalResult = err<number, string>('oh no');
    const explicitUndefined = err<number, undefined>(undefined);
    const implicitVoid = err<number, void>();

    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.

    const arrowValidate = (data: SomeData): Result<number, string> =>
    isValid(data) ? ok(42) : err('something went wrong');

    function fnValidate(data: someData): Result<number, string> {
    return isValid(data) ? ok(42) : err('something went wrong');
    }

    Type Parameters

    • T = never

      The type of the value contained in the Result for the success case

    • E extends string = string

      The type of the error contained in the Result for the failure case

    Parameters

    • err: E

      The value to wrap in a Result.Err.

    Returns Err<T, E>

  • 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 = err<number, string>('something went wrong');
    

    Note: passing nothing will produce a Result<T, void>, passing undefined will produce a Result<T, undefined> which is compatible with Result<T, void>.

    const normalResult = err<number, string>('oh no');
    const explicitUndefined = err<number, undefined>(undefined);
    const implicitVoid = err<number, void>();

    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.

    const arrowValidate = (data: SomeData): Result<number, string> =>
    isValid(data) ? ok(42) : err('something went wrong');

    function fnValidate(data: someData): Result<number, string> {
    return isValid(data) ? ok(42) : err('something went wrong');
    }

    Type Parameters

    • T = never

      The type of the value contained in the Result for the success case

    • E = unknown

      The type of the error contained in the Result for the failure case

    Parameters

    • err: E

      The value to wrap in a Result.Err.

    Returns Err<T, E>

  • 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 = err<number, string>('something went wrong');
    

    Note: passing nothing will produce a Result<T, void>, passing undefined will produce a Result<T, undefined> which is compatible with Result<T, void>.

    const normalResult = err<number, string>('oh no');
    const explicitUndefined = err<number, undefined>(undefined);
    const implicitVoid = err<number, void>();

    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.

    const arrowValidate = (data: SomeData): Result<number, string> =>
    isValid(data) ? ok(42) : err('something went wrong');

    function fnValidate(data: someData): Result<number, string> {
    return isValid(data) ? ok(42) : err('something went wrong');
    }

    Type Parameters

    • T = never

      The type of the value contained in the Result for the success case

    • _E extends void = void

    Parameters

    • err: void

      The value to wrap in a Result.Err.

    Returns Err<T, void>