Class Option<T>

The Option type is an immutable representation of an optional value: every Option is either Some and contains a value, or None and does not.

Type Parameters

  • T = any

Properties

None: None = ...

The None value.

Methods

  • Returns an iterator over the possibly contained value.

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

    Returns Generator<T, void, unknown>

  • Type Parameters

    • B

    Parameters

    Returns Option<B>

    None if the Option is None, otherwise returns optionB.

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

  • Type Parameters

    • B

    Parameters

    • getOptionB: ((value: T) => Option<B>)

      A function that returns an Option

    • 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 Option<B>

    None if the Option is None, otherwise calls getOptionB with the wrapped value and returns the result.

  • Returns None if the Option is None, otherwise calls predicate with the wrapped value and returns:

    • Some(t) if predicate returns true (where t is the wrapped value with inferred new type), and
    • None if predicate returns false.

    Type Parameters

    • U

    Parameters

    • predicate: ((value: T) => value is U)

      A type predicate function that defines type guard by returning true or false.

        • (value): value is U
        • Parameters

          • value: T

          Returns value is 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 Option<U>

  • Returns None if the Option is None, otherwise calls predicate with the wrapped value and returns:

    • Some(t) if predicate returns true (where t is the wrapped value), and
    • None if predicate returns false.

    Parameters

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

      A function that returns true or false.

        • (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 Option<T>

  • Returns boolean

    true if the Option is a None.

  • Whether this value is the same as the other Option.

    Parameters

    • other: unknown

      Another Option or any value

    Returns boolean

    true if the other is an Option and the value are the same as this value via Object.is.

  • Returns boolean

    true if the Option is a Some.

  • Parameters

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

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

        • (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 Option is a Some and and the value inside of it matches a predicate.

  • Maps an Option<T> to Option<U> by applying a function to a contained value (if Some) or returns None (if None).

    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 Option<U>

    None if the Option is None, otherwise returns Some(fn(value)).

  • Extract the value from an Option in a way that handles both the Some and None cases.

    Type Parameters

    • U

    Parameters

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

      A function that returns a value if the Option is a Some.

        • (value): U
        • Parameters

          • value: T

          Returns U

    • None: (() => U)

      A function that returns a value if the Option is a None.

        • (): U
        • Returns U

    Returns U

    The value returned by the provided function.

  • Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).

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

    Type Parameters

    • E

    Parameters

    • error: E

      The error value for Err if the Option is None.

    Returns Result<T, E>

  • Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err()).

    Type Parameters

    • E

    Parameters

    • error: (() => E)

      A function that returns the error value for Err if the Option is None.

        • (): E
        • Returns E

    • 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

    • B

    Parameters

    Returns Option<T | B>

    the Option if it contains a value, otherwise returns optionB.

    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

    • B

    Parameters

    • getOptionB: (() => Option<B>)

      A function that returns an Option

    • 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 Option<T | B>

    the Option if it contains a value, otherwise calls getOptionB and returns the result.

  • Parameters

    • message: string = "called `Option.unwrap()` on a `None` value"

      Optional Error message

    Returns T

    the contained Some value.

    if the value is a None.

  • Returns undefined | T

    the contained Some value or undefined otherwise.

    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

    • defaultValue: U

      default value

    Returns T | U

    the contained Some 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 Some value or computes it from a closure.

  • Unzips an Option containing a tuple of two Options.

    Returns [Option<T extends any[]
        ? T<T>[0]
        : unknown>, Option<T extends any[]
        ? T<T>[1]
        : unknown>]

    [Some(a), Some(b)] if this is Some([a, b]), otherwise [None, None].

  • Type Parameters

    • B

    Parameters

    Returns Option<T | B>

    Some if exactly one of this and optionB is Some, otherwise returns None.

  • Zips this with another Option.

    Type Parameters

    • B

    Parameters

    Returns Option<[T, B]>

    Some([a, b]) if this is Some(a) and other is Some(b), otherwise None.

  • Zips this and another Option with function fn.

    Type Parameters

    • B
    • U

    Parameters

    • optionB: Option<B>
    • fn: ((valueA: T, valueB: B) => U)
        • (valueA, valueB): U
        • Parameters

          • valueA: T
          • valueB: B

          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 Option<U>

    Some(fn(a, b)) if this is Some(a) and other is Some(b), otherwise None.

  • Wrap a value in an Option if the value is truthy.

    Type Parameters

    • T

    Parameters

    • value: T

      A value of type T

    Returns Option<Exclude<T, Falsy>>

  • Wrap a value in an Option if the value satisfies the predicate.

    Type Parameters

    Parameters

    • source: TSource

      Source value

    • predicate: ((source: TSource) => source is T)

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

        • (source): source is T
        • Parameters

          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 Option<T>

  • Wrap a value in an Option if the value satisfies the predicate.

    Type Parameters

    Parameters

    • source: TSource

      Source value

    • predicate: ((source: TSource) => boolean)

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

        • (source): boolean
        • Parameters

          Returns boolean

    • OptionalthisArg: any

    Returns Option<T>

  • Type Parameters

    • T

    Parameters

    • maybeOption: unknown

      A value that might be an Option

    Returns maybeOption is Option<T>

    true if the given value is an Option.

  • Parameters

    • a: unknown

      An Option or any value

    • b: unknown

      An Option or any value

    Returns boolean

    true if the both are Option and the value are the same via Object.is.

  • Type Parameters

    • T = any

    Parameters

    • value: T

      A value of type T

    Returns Option<T>

    Wrap a value into an Option.

  • Type Parameters

    • T

    Parameters

    • valueOrOption: T | Option<T>

      A value of type T or an Option<T>

    Returns undefined | T

    the unwrapOr() result if the value is an Option, otherwise the value itself.