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

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

    const normalResult = ok<number, string>(42);
    const explicitUndefined = ok<undefined, string>(undefined);
    const implicitVoid = ok<void, 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.

    const arrowValidate = (data: SomeData): Result<void, string> =>
    isValid(data) ? ok() : err('something was wrong!');

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

    Type Parameters

    • T

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

    • E = never

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

    Parameters

    • value: T

      The value to wrap in a Result.Ok.

    Returns Ok<T, E>

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

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

    const normalResult = ok<number, string>(42);
    const explicitUndefined = ok<undefined, string>(undefined);
    const implicitVoid = ok<void, 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.

    const arrowValidate = (data: SomeData): Result<void, string> =>
    isValid(data) ? ok() : err('something was wrong!');

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

    Type Parameters

    • _T extends void = void
    • E = never

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

    Parameters

    • value: void

      The value to wrap in a Result.Ok.

    Returns Ok<void, E>