Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Result<T, E>

Represents a success (ok) or failure (err).

Type parameters

  • T

  • E

Hierarchy

  • Result

Index

Methods

and

  • Returns the provided option if this is some, otherwise returns Option.none().

    Type parameters

    • T2

    • E2

    Parameters

    • other: Result<T2, E2>

      The Option to return if this is some.

    Returns Result<T2, E | E2>

andThen

  • andThen<T2, E2>(flatMapper: (value: T) => Result<T2, E2>): Result<T2, E | E2>
  • If this is ok, calls the provided callback with the inner value and returns the callback's return value. Otherwise, returns this.

    The callback is called lazily (i.e., if this is err, the callback will never be called).

    Type parameters

    • T2

    • E2

    Parameters

    • flatMapper: (value: T) => Result<T2, E2>

      A function that returns an Result to return if this is some.

        • Parameters

          • value: T

          Returns Result<T2, E2>

    Returns Result<T2, E | E2>

array

  • array(): [] | [T]
  • Returns an empty array if this is err, otherwise returns a one-item array containing the inner value.

    Similar to Rust's Result::iter().

    Returns [] | [T]

err

  • Returns the inner value wrapped in some if this is err. Otherwise, returns none.

    Returns Option<E>

errSatisfies

  • errSatisfies(predicate: (error: E) => boolean): boolean
  • If this is err, returns the return value of the provided predicate. Otherwise, returns false.

    Parameters

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

          • error: E

          Returns boolean

    Returns boolean

expect

  • expect(message: string): T
  • expect(error: Error): T
  • expect(message: string | Error): T
  • Returns the inner value if this is ok, otherwise throwing an error with the provided message.

    Parameters

    • message: string

      The message of the error to throw if this is err.

    Returns T

  • Returns the inner value if this is ok, otherwise throwing the provided error.

    Parameters

    • error: Error

      The error to throw if this is none.

    Returns T

  • Parameters

    • message: string | Error

    Returns T

expectErr

  • expectErr(message: string): E
  • expectErr(error: Error): E
  • expectErr(message: string | Error): E
  • Returns the inner value if this is err, otherwise throwing an error with the provided message.

    Parameters

    • message: string

      The message of the error to throw if this is ok.

    Returns E

  • Returns the inner value if this is err, otherwise throwing the provided error.

    Parameters

    • error: Error

      The error to throw if this is ok.

    Returns E

  • Parameters

    • message: string | Error

    Returns E

ifErr

  • ifErr(executor: (error: E) => void): void
  • Calls the provided callback if this is err.

    Parameters

    • executor: (error: E) => void

      A callback that will be called if this is err.

        • (error: E): void
        • Parameters

          • error: E

          Returns void

    Returns void

ifOk

  • ifOk(executor: (value: T) => void): void
  • Calls the provided callback with the value that this wraps if this is ok.

    This method is the same as Result.prototype.map() except that it discards the value returned by the callback, unconditionally returning undefined.

    Parameters

    • executor: (value: T) => void

      A callback that will be called if this is ok.

        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns void

isErr

  • isErr(): this is Result<never, E>

isOk

  • isOk(): this is Result<T, never>

map

  • map<T2>(mapper: (value: T) => T2): Result<T2, E>
  • Applies a function to the inner value if this is ok. Otherwise, returns the err untouched.

    Type parameters

    • T2

    Parameters

    • mapper: (value: T) => T2

      A function that will be called if this is ok.

        • (value: T): T2
        • Parameters

          • value: T

          Returns T2

    Returns Result<T2, E>

mapErr

  • mapErr<E2>(mapper: (error: E) => E2): Result<T, E2>
  • Applies a function to the inner value if this is err. Otherwise, returns the ok untouched.

    Type parameters

    • E2

    Parameters

    • mapper: (error: E) => E2

      A function that will be called if this is err.

        • (error: E): E2
        • Parameters

          • error: E

          Returns E2

    Returns Result<T, E2>

match

  • match<U, V>(matcher: { err: (error: E) => V; ok: (value: T) => U }): U | V
  • Accepts an object with two callbacks. One will be called if this is ok. The other will be called if this is err. In either case, the inner value gets passed to the callback.

    Returns the return value of whichever callback gets called.

    Type parameters

    • U

    • V

    Parameters

    • matcher: { err: (error: E) => V; ok: (value: T) => U }

      An object with callbacks for ok and err.

      • err: (error: E) => V
          • (error: E): V
          • Parameters

            • error: E

            Returns V

      • ok: (value: T) => U
          • (value: T): U
          • Parameters

            • value: T

            Returns U

    Returns U | V

ok

  • Returns the inner value wrapped in some if this is ok. Otherwise, returns none.

    Returns Option<T>

okSatisfies

  • okSatisfies(predicate: (value: T) => boolean): boolean
  • If this is ok, returns the return value of the provided predicate. Otherwise, returns false.

    Parameters

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

          • value: T

          Returns boolean

    Returns boolean

or

  • Returns this if this is ok, otherwise returning the provided result.

    Type parameters

    • T2

    • E2

    Parameters

    • other: Result<T2, E2>

      The Result to return if this is err.

    Returns Result<T | T2, E2>

orElse

  • orElse<T2, E2>(otherThunk: (error: E) => Result<T2, E2>): Result<T | T2, E2>
  • Returns this if this is ok, otherwise calling the provided callback and returning its return value.

    Type parameters

    • T2

    • E2

    Parameters

    • otherThunk: (error: E) => Result<T2, E2>

      The callback to call if this is err.

        • Parameters

          • error: E

          Returns Result<T2, E2>

    Returns Result<T | T2, E2>

reverse

  • If this is ok(t), returns err(t). If this is err(e), so this returns ok(e).

    Returns Result<E, T>

safeUnwrap

  • safeUnwrap(this: Result<any, never>): T
  • Same as unwrap() except this method will never throw, since this cannot be err (because the error type is never).

    Parameters

    Returns T

safeUnwrapErr

  • safeUnwrapErr(this: Result<never, any>): E
  • Same as unwrapErr() except this method will never throw, since this cannot be ok (because the ok type is never).

    Parameters

    Returns E

transpose

  • Transposes a Result of an Option into an Option of a Result.

    ok(none) will be mapped to none. ok(some(t)) and err(e) will be mapped to some(ok(t)) and some(err(e)), respectively.

    Type parameters

    • U

    Parameters

    Returns Option<Result<U, E>>

unwrap

  • unwrap(): T
  • Returns the inner value if this is ok, otherwise throwing an error.

    Returns T

unwrapErr

  • unwrapErr(): E
  • Returns the inner value if this is err, otherwise throwing an error.

    Returns E

unwrapErrOrThrowOk

  • unwrapErrOrThrowOk(): E
  • Returns the inner value if this is err, otherwise throws the inner value.

    Returns E

unwrapOr

  • unwrapOr<D>(defaultValue: D): T | D
  • Returns the inner value if this is ok, otherwise returns the provided default.

    Type parameters

    • D

    Parameters

    • defaultValue: D

      The value to return if this is none.

    Returns T | D

unwrapOrElse

  • unwrapOrElse<D>(defaultValueThunk: (error: E) => D): T | D
  • Returns the inner value if this is ok, otherwise calls the provided thunk and returns its return value.

    The thunk is called lazily (i.e., if this is ok, the thunk will never be called because there is no need for a default value).

    Type parameters

    • D

    Parameters

    • defaultValueThunk: (error: E) => D

      A callback that returns the value to return if this is err.

        • (error: E): D
        • Parameters

          • error: E

          Returns D

    Returns T | D

unwrapOrThrowErr

  • unwrapOrThrowErr(): T
  • Returns the inner value if this is ok, otherwise throws the inner value.

    Returns T

Generated using TypeDoc