Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Option<T>

A wrapper representing a value that may be missing.

Type parameters

  • T

Hierarchy

  • Option

Index

Methods

and

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

    Type parameters

    • U

    Parameters

    • other: Option<U>

      The Option to return if this is some.

    Returns Option<U>

andThen

  • andThen<U>(flatMapper: (value: T) => Option<U>): Option<U>
  • If this is some, calls the provided callback with the value that this wraps and returns the callback's return value. Otherwise, returns Option.none().

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

    Type parameters

    • U

    Parameters

    • flatMapper: (value: T) => Option<U>

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

        • Parameters

          • value: T

          Returns Option<U>

    Returns Option<U>

array

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

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

    Returns T[]

equalsSome

  • equalsSome(other: T): boolean
  • Returns false if this is none, otherwise returns whether the value that this wraps equals the provided value (equality is determined using ===).

    In other words, this method returns true if and only if this is some(v) and v === other.

    Parameters

    • other: T

    Returns boolean

expect

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

    Parameters

    • message: string

      The message of the UnwrapError to throw if this is none.

    Returns T

  • Returns the value that this wraps if this is some, otherwise throwing the provided error.

    Parameters

    • error: Error

      The error to throw if this is none.

    Returns T

  • Parameters

    • message: string | Error

    Returns T

filter

  • filter(predicate: (value: T) => boolean): Option<T>
  • Returns Option.none() if this is none, otherwise calls the provided predicate with the value that this wraps and returns this if the predicate returns true and Option.none() if the predicate returns false.

    Parameters

    • predicate: (value: T) => boolean

      The callback that returns whether to keep the wrapped value.

        • (value: T): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Option<T>

flatten

  • Converts from Option<Option<U>> to Option<U>. Only removes one level of nesting.

    Type parameters

    • U

    Parameters

    Returns Option<U>

ifNone

  • ifNone(executor: () => void): void
  • Calls the provided callback if this is none.

    Parameters

    • executor: () => void

      A callback that will be called if this is none.

        • (): void
        • Returns void

    Returns void

ifSome

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

    This method is the same as Option.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 some.

        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns void

isNone

  • isNone(): boolean

isSome

  • isSome(): boolean

map

  • map<R>(mapper: (value: T) => R): Option<R>
  • Returns Option.none() if this is none, and Option.some(mapper(x)) where x is the value that this wraps.

    Type parameters

    • R

    Parameters

    • mapper: (value: T) => R

      A function that will be called if this is some.

        • (value: T): R
        • Parameters

          • value: T

          Returns R

    Returns Option<R>

match

  • match<N, S>(matcher: { none: () => N; some: (value: T) => S }): N | S
  • Accepts an object with two callbacks. One will be called if this is none. The other will be called with the value that this wraps if this is some.

    Returns the return value of whichever callback gets called.

    Type parameters

    • N

    • S

    Parameters

    • matcher: { none: () => N; some: (value: T) => S }

      An object with callbacks for none and some.

      • none: () => N
          • (): N
          • Returns N

      • some: (value: T) => S
          • (value: T): S
          • Parameters

            • value: T

            Returns S

    Returns N | S

or

  • Returns this if this is some, otherwise returning the provided option.

    Type parameters

    • U

    Parameters

    • other: Option<U>

      The Option to return if this is none.

    Returns Option<T | U>

orElse

  • Returns this if this is some, otherwise calling the provided callback and returning its return value.

    Type parameters

    • U

    Parameters

    • otherThunk: () => Option<U>

      The callback to call if this is none.

    Returns Option<T | U>

someSatisfies

  • someSatisfies(predicate: (val: T) => boolean): boolean
  • Returns false if this is none, otherwise returns whether the value that this wraps satisfies the provided predicate.

    In other words, this method returns true if and only if this is some(v) and predicate(v) is true.

    Parameters

    • predicate: (val: T) => boolean

      The callback to call if this is some.

        • (val: T): boolean
        • Parameters

          • val: T

          Returns boolean

    Returns boolean

transpose

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

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

    Type parameters

    • U

    • E

    Parameters

    Returns Result<Option<U>, E>

unwrap

  • unwrap(): T
  • Returns the value that this wraps if this is some, otherwise throwing an UnwrapError.

    Returns T

unwrapOr

  • unwrapOr<D>(defaultValue: D): T | D
  • Returns the value that this wraps if this is some, 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: () => D): T | D
  • Returns the value that this wraps if this is some, otherwise calls the provided thunk and returns its return value.

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

    Type parameters

    • D

    Parameters

    • defaultValueThunk: () => D

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

        • (): D
        • Returns D

    Returns T | D

xor

  • Returns the Option that is some if exactly one of [this, other] is some, otherwise returns Option.none().

    Type parameters

    • U

    Parameters

    Returns Option<T | U>

Generated using TypeDoc