Class Result<T, E>

The Result type is an immutable representation of either success (Ok) or failure (Err).

Type Parameters

  • T = any
  • E = any

Methods

  • Returns an iterator over the possibly contained value.

    The iterator yields one value if the result is Ok, otherwise none.

    Returns Generator<T, void, unknown>

  • Type Parameters

    • BT
    • BE = any

    Parameters

    • getResultB: ((value: T) => Result<BT, BE>)

      A function that returns a Result

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns Result<BT, E | BE>

    Err if the Result is Err, otherwise calls getOptionB with the wrapped value and returns the result.

  • Returns boolean

    true if the Result is an Err.

  • Parameters

    • predicate: ((error: E) => boolean)
        • (error): boolean
        • Parameters

          • error: E

          Returns boolean

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns boolean

    true if the Result is an Err and and the error inside of it matches a predicate.

  • Returns boolean

    true if the Result is an Ok.

  • Parameters

    • predicate: ((value: T) => boolean)
        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns boolean

    true if the Result is an Ok and and the value inside of it matches a predicate.

  • Whether this Ok value or Err error is the same as the other Result.

    Parameters

    • other: unknown

      Another Result or any value

    Returns boolean

    true if the other is an Result and the Ok value or Err error is the same as this via Object.is.

  • Maps an Result<T, E> to Result<U, E> by applying a function to a contained Ok value, leaving an Err value untouched.

    Type Parameters

    • U

    Parameters

    • fn: ((value: T) => U)

      A function that maps a value to another value

        • (value): U
        • Parameters

          • value: T

          Returns U

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns Result<U, E>

    Err if the Result is Err, otherwise returns Ok(fn(value)).

  • Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value untouched.

    This function can be used to pass through a successful result while handling an error.

    Type Parameters

    • U

    Parameters

    • fn: ((error: E) => U)

      A function that maps a error to another error

        • (error): U
        • Parameters

          • error: E

          Returns U

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns Result<T, U>

    Ok if the Result is Ok, otherwise returns Err(fn(error)).

  • Extract the value from an Result in a way that handles both the Ok and Err cases.

    Type Parameters

    • U

    Parameters

    • Ok: ((value: T) => U)

      A function that returns a value if the Result is a Ok.

        • (value): U
        • Parameters

          • value: T

          Returns U

    • Err: ((error: E) => U)

      A function that returns a value if the Result is a Err.

        • (error): U
        • Parameters

          • error: E

          Returns U

    Returns U

    The value returned by the provided function.

  • Type Parameters

    • BT
    • BE = any

    Parameters

    Returns Result<T | BT, E | BE>

    the Result if it is Ok, otherwise returns resultB.

    Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use orElse, which is lazily evaluated.

  • Type Parameters

    • BT
    • BE = any

    Parameters

    • getResultB: (() => Result<BT, BE>)

      A function that returns an Result

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns Result<T | BT, E | BE>

    the Result if it contains a value, otherwise calls getResultB and returns the result.

  • Parameters

    • message: string = "called `Result.unwrap()` on an `Err`"

      Optional Error message

    Returns T

    the contained Ok value.

    if the value is an Err.

  • Parameters

    • message: string = "called `Result.unwrapErr()` on an `Ok` value"

      Optional Error message

    Returns E

    the contained Err error.

    if the error is an Ok.

  • Returns undefined | E

    the contained Err error or undefined otherwise.

    Arguments passed to unwrapErrOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrapErrOrElse, which is lazily evaluated.

  • Type Parameters

    • U

    Parameters

    • defaultError: U

      default error

    Returns E | U

    the contained Err error or a provided default.

    Arguments passed to unwrapErrOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrapErrOrElse, which is lazily evaluated.

  • Type Parameters

    • U

    Parameters

    • fn: (() => U)

      A function that computes a default value.

        • (): U
        • Returns U

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns E | U

    the contained Err error or computes it from a closure.

  • Returns undefined | T

    the contained Ok value or undefined otherwise.

  • Type Parameters

    • U

    Parameters

    • defaultValue: U

      default value

    Returns T | U

    the contained Ok value or a provided default.

    Arguments passed to unwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrapOrElse, which is lazily evaluated.

  • Type Parameters

    • U

    Parameters

    • fn: (() => U)

      A function that computes a default value.

        • (): U
        • Returns U

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns T | U

    the contained Ok value or computes it from a closure.

  • Type Parameters

    • E
    • T = any

    Parameters

    • error: E

      An error of type E

    Returns Result<T, E>

    Wrap an error into an Result.

  • Err if the value is an Error.

    Type Parameters

    • T = any
    • E extends Error = Error

    Parameters

    • source: T | E

    Returns Result<T, E>

  • OK if the value satisfies the predicate, otherwise Err

    Type Parameters

    • T = any
    • E = any

    Parameters

    • source: T | E

      Source value

    • predicate: ((source: T | E) => source is T)

      A function that returns true if the value satisfies the predicate, otherwise false

        • (source): source is T
        • Parameters

          • source: T | E

          Returns source is T

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns Result<T, E>

  • OK if the value satisfies the predicate, otherwise Err

    Type Parameters

    • T = any
    • E = any

    Parameters

    • source: T | E

      Source value

    • predicate: ((source: T | E) => boolean)

      A function that returns true if the value satisfies the predicate, otherwise false

        • (source): boolean
        • Parameters

          • source: T | E

          Returns boolean

    • OptionalthisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns Result<T, E>

  • Type Parameters

    • T
    • E

    Parameters

    • maybeResult: unknown

      A value that might be an Result

    Returns maybeResult is Result<T, E>

    true if the given value is an Result.

  • Parameters

    • a: unknown

      An Result or any value

    • b: unknown

      An Result or any value

    Returns boolean

    true if the both are Result and the Ok value or Err error are the same via Object.is.

  • Type Parameters

    • T
    • E = any

    Parameters

    • value: T

      A value of type T

    Returns Result<T, E>

    Wrap a value into an Result.

  • Ok if the fn returns a value, Err if the fn throws.

    Type Parameters

    • T = any
    • E = any
    • TArgs extends any[] = []

    Parameters

    • fn: ((...args: TArgs) => T)
        • (...args): T
        • Parameters

          Returns T

    • Rest...args: TArgs

    Returns Result<T, E>

    Ok with the returned value or Err with the exception error.

  • Ok if the fn returned Promise resolves a value, Err if the fn throws or the Promise rejected.

    Type Parameters

    • T = any
    • E = any
    • TArgs extends any[] = []

    Parameters

    • fn: ((...args: TArgs) => T)
        • (...args): T
        • Parameters

          Returns T

    • Rest...args: TArgs

    Returns Promise<Result<T, E>>

    Ok with the resolved value or Err with the exception error or the rejected value.