Class Optional<T>

Wrapper for a value that might or might not be present.

Type Parameters

  • T

Methods

  • Create an empty Optional instance

    Type Parameters

    • T

    Returns Optional<Exclude<T, undefined | null>>

    An empty Optional

  • Filter the value (if present)

    Parameters

    • filter: (value: T) => unknown

    Returns Optional<T>

  • Perform an action if the Optional does not contain a value

    Parameters

    • action: () => void

    Returns this

  • Perform an action if the Optional contains a value

    Parameters

    • action: (value: T) => void

    Returns this

  • Check if the Optional is empty

    Returns boolean

  • Check if the Optional contains a value

    Returns this is Optional<T> & { get: () => T }

  • Map the value (if present)

    Type Parameters

    • R

    Parameters

    • map: (value: T) => R

    Returns Optional<R>

  • Create an optional

    Type Parameters

    • T

    Parameters

    • value: T

    Returns Optional<Exclude<T, undefined | null>>

  • Get the value (if present) or fail with a an internal error

    Parameters

    • message: string

    Returns T

  • Get the value (if present) or return the specified default value otherwise

    Type Parameters

    • U

    Parameters

    • fallbackValue: U

    Returns T | U

  • Get the value (if present) or fail with a user-friendly error message

    Parameters

    • message: string

    Returns T

  • Get the value (if present) or call the specified function and return its return value otherwise

    Type Parameters

    • U

    Parameters

    • get: () => U

    Returns T | U