A type which represents values that may or may not be present, without using undefined or null.

Type Parameters

  • T extends NonNullable<unknown>

    if this Option contains a value, this is the type of the value

Boolean operators

Constructing

Extracting the contained value

Interacting with Result

Querying the variant

Transforming contained values

Boolean operators

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

    Type Parameters

    • U extends {}

      the type contained in the Option returned by fn

    Parameters

    • this: Option<T>
    • fn: ((value) => Option<U>)

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

    Returns Option<U>

    the result of calling fn if this is Some, None otherwise

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

    Type Parameters

    • U extends {}

      the type contained in the Option returned by fn

    Parameters

    • this: Option<T>
    • fn: ((value) => Promise<Option<U>>)

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

    Returns Promise<Option<U>>

    the result of calling fn if this is Some, None otherwise

  • Takes a function that returns a promise which resolves to a new Option; calls it and returns the result if this is None, otherwise returns a promise which resolves to this.

    Parameters

    • this: Option<T>
    • fn: (() => Promise<Option<T>>)

      a function that will be called if this is None

    Returns Promise<Option<T>>

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

Constructing

  • Takes a value that could be null or undefined and converts it into an Option.

    Type Parameters

    • T

      the type contained by the Option values

    Parameters

    • value: T

      the nullable value

    Returns Option<NonNullable<T>>

    None if value is null or undefined, Some containing value otherwise

    See

Extracting the contained value

  • Takes two functions, one is called with the contained value if this is Some and the other is called if this is None.

    Type Parameters

    • U

      the type of the return value of the matcher functions

    Parameters

    • this: Option<T>
    • matchers: {
          None: (() => U);
          Some: ((value) => U);
      }

      an object containing matcher functions

      • None: (() => U)

        A function that will be called if this is None.

        Returns

        an arbitrary value

          • (): U
          • Returns U

      • Some: ((value) => U)

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

        Returns

        an arbitrary value

          • (value): U
          • Parameters

            • value: T

              the value contained in the Some

            Returns U

    Returns U

    the return value of the Some or None matcher function

  • Returns the contained value if this is Some, otherwise return the provided default value.

    Parameters

    • this: Option<T>
    • defaultValue: T

      the value to return if this is None

    Returns T

    the contained if this is Some, otherwise defaultValue

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

    Parameters

    • this: Option<T>
    • defaultValue: (() => T)

      a function that will be called if this is None

        • (): T
        • Returns T

    Returns T

    the contained if this is Some, otherwise the return value from calling defaultValue

Interacting with Result

  • If this is a Some, return an Result.Ok with the contained value. Otherwise, return an Result.Err containing the passed default error value.

    Type Parameters

    • E extends {}

      the type of error (and the error type of the returned Result)

    Parameters

    • this: Option<T>
    • error: E

      an error value to use if this is None

    Returns Result<T, E>

    Result.Ok containing the value in this if this is Some, otherwise Result.Err containing error

  • If this is a Some, return an Result.Ok with the contained value. Otherwise, return an Result.Err with the value returned by calling the passed function.

    Type Parameters

    • E extends {}

      the return type of fn (and the error type of the returned Result)

    Parameters

    • this: Option<T>
    • error: (() => E)

      a function which returns an error value to use if this is None

        • (): E
        • Returns E

    Returns Result<T, E>

    Result.Ok containing the value in this if this is Some, otherwise Result.Err containing the value returned by error

Querying the variant

  • Check if this is None.

    Parameters

    Returns boolean

    true if this is None, false otherwise

    See

    isSome

  • Check if this is Some.

    Parameters

    Returns boolean

    true if this is Some, false otherwise

    See

    isNone

Transforming contained values

  • If this is None, returns None. Otherwise, calls the predicate on the value contained in this; if the predicate returns true then this is returned, if the predicate returns false then None is returned.

    Parameters

    • this: Option<T>
    • fn: ((value) => boolean)

      the predicate function

        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Option<T>

    None if this is None or if fn returns false when applied to the value in this, this otherwise

  • Transforms Option<T> to Option<U> by applying the provided function to the contained value of Some and leaving None values unchanged.

    Type Parameters

    • U extends {}

      the type of the return value of fn

    Parameters

    • this: Option<T>
    • fn: ((value) => U)

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

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Option<U>

    None if this is None, otherwise Some containing the result of applying fn to the value in this

  • Transforms Option<T> to Promise<Option<U>> by applying the provided async function to the contained value of Some and resolving None values unchanged.

    Type Parameters

    • U extends {}

      the type of the value in the promise returned by fn

    Parameters

    • this: Option<T>
    • fn: ((value) => Promise<U>)

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

        • (value): Promise<U>
        • Parameters

          • value: T

          Returns Promise<U>

    Returns Promise<Option<U>>

    a promise resolving to None if this is None, otherwise a promise resolving to Some 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 Some, otherwise returns the provided default value.

    Type Parameters

    • U extends {}

      the type of defaultValue and the value returned by fn

    Parameters

    • this: Option<T>
    • defaultValue: U

      the value to return if this is None

    • fn: ((value) => U)

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

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

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

  • Called on an Option containing a tuple -- if this is Some, returns a tuple of Some containing the contained values. Otherwise, returns a tuple of None.

    Type Parameters

    • T extends {}

      the type of the first value in the tuple in this

    • U extends {}

      the type of the second value in the tuple in this

    Parameters

    Returns readonly [Option<T>, Option<U>]

    a tuple of Some containing the values in the tuple contained in this if this is Some, a tuple of None otherwise

  • Takes another Option, if both Option values are Some, returns a Some containing a tuple of the contained values. Otherwise returns None.

    Type Parameters

    • U extends {}

      the type of the value in other

    Parameters

    Returns Option<readonly [T, U]>

    Some containing a tuple with the values in this and other if both this and other are Some, None otherwise

  • Takes an Array of Option values. If any of the Option values in the array is None, returns None. Otherwise, returns Some containing an Array of each value inside each Option in the original list.

    Type Parameters

    • T extends {}

      the type contained by the Option values

    Parameters

    Returns Option<T[]>

    Some containing an Array of values contained by each Option in the original Array if all Option values in the original list are Some, None otherwise