Function ok

  • 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');
    }

    Type Parameters

    • T extends {}

    • E

    Returns Result<Unit, E>

    Typeparam

    T The type of the item contained in the Result.

  • 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');
    }

    Type Parameters

    • T

    • E

    Parameters

    • value: T

      The value to wrap in a Result.Ok.

    Returns Result<T, E>

    Typeparam

    T The type of the item contained in the Result.

Generated using TypeDoc