Class Optional<T>

Type Parameters

  • T

Constructors

Properties

value: undefined | T

Methods

  • Converts the internal type to another. guard is called to do the necessary type checking. This is generally used for up- and down-casting.

    Type Parameters

    • S

    Parameters

    • guard: ((o) => boolean)
        • (o): boolean
        • Parameters

          Returns boolean

    Returns Optional<S>

  • Compares this Optional value to the provided on.

    Empty Optionals are never considered equal to anything, including other empty Optionals.

    Parameters

    • val: Optional<T>
    • isEqual: ((a, b) => boolean) = strictEquals

      an optional function for comparing equality

        • (a, b): boolean
        • Parameters

          Returns boolean

    Returns boolean

  • Checks the value of the optional. If it matches the predicate, then a non-empty Optional of the same value will be returned.

    Otherwise, an empty Optional is returned.

    Parameters

    • predicate: ((v) => boolean)
        • (v): boolean
        • Parameters

          Returns boolean

    Returns Optional<T>

  • Map the value of an Optional to an Optional value of the same or a different type.

    This differs from map, such that the returned Optional is 'squashed' so that the returned value is Optional<U> not Optional<Optional<U>>

    Type Parameters

    • U

    Parameters

    • mapper: ((val) => Optional<undefined | null | U>)

    Returns Optional<U>

  • Get the value of the optional or throw a NoSuchElement exception

    Returns T

  • If the Optional is non-empty, pass the value to a consuming function.

    The consumer should not return a value. If it does, this value is ignored.

    Parameters

    • consumer: ((val) => void)
        • (val): void
        • Parameters

          • val: T

          Returns void

    Returns {
        orElse: ((f) => void);
    }

    • orElse: ((f) => void)
        • (f): void
        • Parameters

          • f: (() => void)
              • (): void
              • Returns void

          Returns void

  • Check whether the Optional is empty or not.

    Returns boolean

  • Return the Optional's value if it is not empty, otherwise return the provided value.

    Parameters

    • other: T

    Returns T

  • Return the Optional's value if it is not empty, otherwise call other and return that value.

    Parameters

    • other: (() => T)
        • (): T
        • Returns T

    Returns T

  • Return the Optional's value if it is not empty, otherwise call exceptionSupplier to generate an exception that is then thrown

    Parameters

    • exceptionSupplier: (() => Error)
        • (): Error
        • Returns Error

    Returns T

  • Return the Optional's value if it is not empty, otherwise return undefined.

    Returns undefined | T

  • Return the Optional's value if it is not empty, otherwise return null.

    Returns null | T