Class Ok<T>

Stores the result of a successful operation. Access the contained via Ok.value. Refer to ResultBase for available methods.

See

ResultBase

Type Parameters

  • T

Implements

Constructors

Properties

value: T

Methods

  • Returns the contained value if this Result is Ok, otherwise throws with the given error message.

    Parameters

    • msg: string

      The error message to be displayed if this Result is Err.

    Returns T

    The contained value if this Result is Ok.

    Throws

    An error with the given error message if this Result is Err. The error message includes the provided message, as well as the content of the containing error.

  • Returns the contained error value if this Result is Err, otherwise throws with the given error message.

    Parameters

    • message: string

      The error message to be displayed if this Result is Ok.

    Returns never

    The contained error value if this Result is Err.

    Throws

    An error with the given error message and the content of the containing error as the message if this Result is Ok.

  • Calls the provided function with the contained value if this Result is Ok. Nothing is performed otherwise.

    Parameters

    • f: ((value) => unknown)

      The function to be called if this Result is Ok. First arg will receive the contained value.

        • (value): unknown
        • Parameters

          • value: T

          Returns unknown

    Returns this

    This Result.

  • Calls the provided function with the contained value if this Result is Err. Nothing is performed otherwise.

    Parameters

    • f: ((error) => unknown)

      The function to be called if this Result is Err. First arg will receive the contained error.

        • (error): unknown
        • Parameters

          • error: never

          Returns unknown

    Returns this

    This Result.

  • A type guard that checks if this Result is Err. If it is, the Result is cast to Err.

    Returns this is Err<never>

    true if this Result is Err, false otherwise.

  • A type guard that checks if this Result is Ok. If it is, the Result is cast to Ok.

    Returns this is Ok<T>

    true if this Result is Ok, false otherwise.

  • If this Result is Ok, calls the given mapper function with the current contained value and returns a new Result containing the value returned by the mapper function.

    Type Parameters

    • TNew

    Parameters

    • mapper: ((value) => TNew)

      The function that maps the current contained value to another value.

    Returns Ok<TNew>

    A new Result object containing the value returned by the mapper function, or this if this is Err.

  • If this Result is Err, calls the given mapper with the current error value, then returns a new Result containing the new error value returned by the mapper. Otherwise, nothing is performed and this is returned.

    Type Parameters

    • TNewErr

    Parameters

    • mapper: ((error) => TNewErr)

      The function that maps the contained error value to another error value.

    Returns this

    A new Result containing the error value returned by the mapper if this is Err, or this Result otherwise.

  • If this Result is Ok, it does the same thing as map. Otherwise, returns the given default value.

    Type Parameters

    • TNew
    • TDef

    Parameters

    • def: TDef

      The default value to be returned if this Result is Err.

    • mapper: ((result) => TNew)

      The function that maps the current contained value to another value if this Result is Ok.

        • (result): TNew
        • Parameters

          • result: T

          Returns TNew

    Returns TNew

    A new Result object containing the value returned by the mapped function if this is Ok, otherwise the default value.

  • Same as mapOr, except if this is Err, the default value is returned lazily by the first callback which receives the contained error and returns the default value.

    Type Parameters

    • TNew
    • TDef

    Parameters

    • errMapper: ((error) => TDef)

      The function that maps the contained error value to a default value if this Result is Err.

        • (error): TDef
        • Parameters

          • error: never

          Returns TDef

    • mapper: ((value) => TNew)

      The function that maps the contained value to a new value if this Result is Ok.

    Returns TNew

    The value returned by the error mapper if this is Err; The value returned by the value mapper if this is Ok.

  • Calls the given callback with the contained error value if this Result is Err.

    Type Parameters

    Parameters

    • op: ((error) => TR)
        • (error): TR
        • Parameters

          • error: never

          Returns TR

    Returns this

    the Result returned by the given callback, otherwise this Result

  • Returns T

    the contained value if this Result is Ok.

    Throws

    if this Result is Err, with the error message provided by the containing error.

  • Returns never

    the contained error value if this Result is Err.

    Throws

    the contained value as the error message if this Result is Ok

  • Calls the given function with the contained error value and returns the value returned by the function if this Result is Err. Otherwise, returns the contained value.

    Type Parameters

    • TDef

    Parameters

    • op: ((error) => TDef)
        • (error): TDef
        • Parameters

          • error: never

          Returns TDef

    Returns T

    The contained value if this Result is Ok or the value returned by the given function otherwise.