A type which represents values that may encode either a successful result or an error result.

Type Parameters

  • T extends NonNullable<unknown>

    the type of successful results

  • E extends NonNullable<unknown> = Error

    the type of error results

Boolean operators

Constructing

Extracting the contained value

Interacting with Option

Querying the variant

Transforming contained values

Boolean operators

  • Takes a function which takes the value contained in this and returns a new Result; calls it and returns the result if this is Ok, otherwise returns this. Also sometimes known in other languages or libraries as flatmap or bind.

    Type Parameters

    • U extends {}

      the type contained in the Result returned by fn

    Parameters

    • this: Result<T, E>
    • fn: ((value) => Result<U, E>)

      a function that will be called with the value in this if this is Ok

    Returns Result<U, E>

    the result of calling fn if this is Ok, this otherwise

  • Takes a function which takes the value contained in this and returns a promise which resolves to a new Result; calls it and returns the result if this is Ok, otherwise returns a promise which resolves to this. This is an async version of andThen.

    Type Parameters

    • U extends {}

      the type contained in the Result returned by fn

    Parameters

    • this: Result<T, E>
    • fn: ((value) => Promise<Result<U, E>>)

      a function that will be called with the value in this if this is Ok

    Returns Promise<Result<U, E>>

    the result of calling fn if this is Ok, this otherwise

  • Takes a function which takes an error and returns a Result; calls it on the contained error and returns the result if this is Err, otherwise returns this.

    Type Parameters

    • F extends {} = Error

    Parameters

    • this: Result<T, E>
    • fn: ((error) => Result<T, F>)

      a function that will be called with the error value if this is Err

    Returns Result<T, F>

    the result of calling fn if this is Err, this otherwise

  • Takes a function which takes an error and returns a promise which resolves to a new Result; calls it with the contained error and returns the result if this is Err, otherwise returns a promise which resolves to this.

    Type Parameters

    • F extends {} = Error

    Parameters

    • this: Result<T, E>
    • fn: ((error) => Promise<Result<T, F>>)

      a function that will be called if this is Err

    Returns Promise<Result<T, F>>

    the result of calling fn if this is Err, this otherwise

Constructing

  • Construct a Result containing an error value.

    Type Parameters

    • T extends {}

      the type of success values

    • E extends {} = Error

      the type of the error value

    Parameters

    • error: E

      the error contained by the Result

    Returns Result<T, E>

    a Result containing an error error

  • Construct a Result containing a success value.

    Type Parameters

    • T extends {}

      the type of the value

    • E extends {} = Error

      the type of error results

    Parameters

    • value: T

      the value contained by the Result

    Returns Result<T, E>

    a Result containing value

  • Takes a function that could be throw and converts it into an Result.

    Type Parameters

    • T extends {}

      the type contained by the Result

    Parameters

    • fn: (() => T)

      the function which could throw

        • (): T
        • Returns T

    Returns Result<T, Option<{}>>

    Ok with the return value of fn if fn doesn't throw, otherwise Err containing an Option which is None if the exception is null or undefined, and is Some with the exception otherwise

  • Takes a promise that could reject and converts it into an Result.

    Type Parameters

    • T extends {}

      the type contained by the Result

    Parameters

    • promise: Promise<T>

      the promise to convert

    Returns Promise<Result<T, Option<{}>>>

    a Promise containing Ok with the value resolved by promise if promise doesn't reject, otherwise Err containing an Option which is None if promise rejects with a null or undefined, and is Some with the rejection value otherwise

    See

Extracting the contained value

  • Takes two functions, one is called with the contained value if this is Ok and the other is called with the error if this is Err.

    Type Parameters

    • U

      the type of the return value of the matcher functions

    Parameters

    • this: Result<T, E>
    • matchers: {
          Err: ((error) => U);
          Ok: ((value) => U);
      }

      an object containing matcher functions

      • Err: ((error) => U)

        A function that will be called on the value contained if this is Err.

        Returns

        an arbitrary value

          • (error): U
          • Parameters

            • error: E

              the error contained in the Err

            Returns U

      • Ok: ((value) => U)

        A function that will be called on the value contained if this is Ok.

        Returns

        an arbitrary value

          • (value): U
          • Parameters

            • value: T

              the value contained in the Ok

            Returns U

    Returns U

    the return value of the Ok or Err matcher function

  • Returns the contained value if this is Ok, otherwise return the provided default value, ignoring any error value.

    Parameters

    • this: Result<T, E>
    • defaultValue: T

      the value to return if this is Err

    Returns T

    the contained if this is Ok, otherwise defaultValue

  • Returns the contained value if this is Ok, otherwise call the provided function with the error value and return the result.

    Parameters

    • this: Result<T, E>
    • defaultValue: ((error) => T)

      a function that will be called on the error value if this is Err

        • (error): T
        • Parameters

          • error: E

          Returns T

    Returns T

    the contained if this is Ok, otherwise the return value from calling defaultValue on the error value

Interacting with Option

Querying the variant

  • Check if this is Err.

    Parameters

    Returns boolean

    true if this is Err, false otherwise

    See

    isOk

  • Check if this is Ok.

    Parameters

    Returns boolean

    true if this is Ok, false otherwise

    See

    isErr

Transforming contained values

  • Transforms Result<T, E> to Result<U, E> by applying the provided function to the contained value of Ok and leaving Err values unchanged.

    Type Parameters

    • U extends {}

      the type of the return value of fn

    Parameters

    • this: Result<T, E>
    • fn: ((value) => U)

      the function to apply to the value contained if this is Ok

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Result<U, E>

    this if this is Err, otherwise Ok containing the result of applying fn to the value in this

  • Transforms Result<T, E> to Promise<Result<U, E>> by applying the provided async function to the contained value of Ok and resolving Err values unchanged.

    Type Parameters

    • U extends {}

      the type of the value in the promise returned by fn

    Parameters

    • this: Result<T, E>
    • fn: ((value) => Promise<U>)

      the function to apply to the value contained if this is Ok

        • (value): Promise<U>
        • Parameters

          • value: T

          Returns Promise<U>

    Returns Promise<Result<U, E>>

    a promise resolving to this if this is Err, otherwise a promise resolving to Ok containing the value resolved by the promise returned from applying fn to the value in this

  • Transforms Result<T, E> to Result<T, F> by applying the provided function to the contained value of Err and leaving Ok values unchanged.

    Type Parameters

    • F extends {} = Error

      the type of the return value of fn

    Parameters

    • this: Result<T, E>
    • fn: ((error) => F)

      the function to apply to the value contained if this is Err

        • (error): F
        • Parameters

          • error: E

          Returns F

    Returns Result<T, F>

    this if this is Ok, otherwise Err containing the result of applying fn to the value in this

  • Transforms Result<T, E> to Promise<Result<T, F>> by applying the provided async function to the contained value of Err and resolving Ok values unchanged.

    Type Parameters

    • F extends {} = Error

      the type of the value in the promise returned by fn

    Parameters

    • this: Result<T, E>
    • fn: ((error) => Promise<F>)

      the function to apply to the value contained if this is Err

        • (error): Promise<F>
        • Parameters

          • error: E

          Returns Promise<F>

    Returns Promise<Result<T, F>>

    a promise resolving to this if this is Ok, otherwise a promise resolving to Err containing the value resolved by the promise returned from applying fn to the value in this

  • Applies the provided function to the contained value if this is Ok, otherwise returns the provided default value.

    Type Parameters

    • U extends {}

      the type of defaultValue and the value returned by fn

    Parameters

    • this: Result<T, E>
    • defaultValue: U

      the value to return if this is Err

    • fn: ((value) => U)

      the function to apply to the value contained if this is Ok

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

    the result of applying fn to the value in this if this is Ok, otherwise defaultValue

  • Takes an Array of Result values. If any of the Result values in the array is Err, returns that Err. Otherwise, returns Ok containing an Array of each value inside each Result in the original list.

    Type Parameters

    • T extends {}

      the type contained by the Result values

    • E extends {} = Error

      the error type of the Result values

    Parameters

    Returns Result<T[], E>

    Ok containing an Array of values contained by each Result in the original Array if all Result values in the original list are Ok, otherwise returns the first Err in the Array