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();
}

The type of the item contained in the Result.

The error value to wrap in a Result.Err.

  • Create an instance of Err.

    Note: While you may create the Result type via normal JavaScript class construction, it is not recommended for the functional style for which the library is intended. Instead, use err.

    // Avoid:
    const anErr = new Result.Err('alas, failure');

    // Prefer:
    const anErr = Result.err('alas, failure');

    Type Parameters

    • T = never
    • E = never

    Returns Result<T, Unit>

  • Create an instance of Err.

    Note: While you may create the Result type via normal JavaScript class construction, it is not recommended for the functional style for which the library is intended. Instead, use err.

    // Avoid:
    const anErr = new Result.Err('alas, failure');

    // Prefer:
    const anErr = Result.err('alas, failure');

    Type Parameters

    • T = never
    • E = never

    Parameters

    • error: E

      The value to wrap in an Err.

    Returns Result<T, E>