Create an instance of Ok.

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 yayNumber = Result.ok<number, string>(12);

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

const normalResult = Result.ok<number, string>(42);
const explicitUnit = Result.ok<Unit, string>(Unit);
const implicitUnit = Result.ok<Unit, string>();

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<Unit, string> =>
isValid(data) ? Result.ok() : Result.err('something was wrong!');

function fnValidate(data: someData): Result<Unit, string> {
return isValid(data) ? Result.ok() : Result.err('something was wrong');
}

The type of the item contained in the Result.

The value to wrap in a Result.Ok.

  • Create an instance of Ok.

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

    // Avoid:
    const aString = new Result.Ok('characters');

    // Prefer:
    const aString = Result.ok('characters);

    Note that you may explicitly pass Unit to the Ok constructor to create a Result<Unit, E>. However, you may not call the Ok constructor with null or undefined to get that result (the type system won't allow you to construct it that way). Instead, for convenience, you can simply call ok, which will construct the type correctly.

    Type Parameters

    • T extends {}
    • E = never

    Returns Result<Unit, E>

  • Create an instance of Ok.

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

    // Avoid:
    const aString = new Result.Ok('characters');

    // Prefer:
    const aString = Result.ok('characters);

    Note that you may explicitly pass Unit to the Ok constructor to create a Result<Unit, E>. However, you may not call the Ok constructor with null or undefined to get that result (the type system won't allow you to construct it that way). Instead, for convenience, you can simply call ok, which will construct the type correctly.

    Type Parameters

    • T
    • E = never

    Parameters

    • value: T

      The value to wrap in an Ok.

    Returns Result<T, E>