typefinity
    Preparing search index...

    Class Optional<T>

    Wrapper for an optional value that might or might not be present

    Type Parameters

    • T
    Index

    Methods

    • Create an empty Optional instance

      Type Parameters

      • T

      Returns Optional<Exclude<T, null>>

      An empty Optional

    • Filter the value (if present)

      Parameters

      • filter: (value: T) => unknown

        A predicate that returns a truthy value (if the Optional's value should be preserved) or a falsy value (if the Optional's value should be discarded)

      Returns Optional<T>

      The current Optional (if it is not empty and if the filter returns a truthy value) or an empty Optional (if if the current Optional is empty or if the filter returns a falsy value)

    • Map the value (if present) to another Optional and unwrap it

      Type Parameters

      • R

      Parameters

      • map: (value: T) => Optional<R>

        A function that maps the current Optional's value to another Optional

      Returns Optional<R>

      The Optional returned by the map function (if the current Optional is present) or an empty optional (if the current Optional is empty)

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

      Parameters

      • action: () => void

        The action to perform

      Returns this

      The current Optional

    • Perform an action if the Optional contains a value

      Parameters

      • action: (value: T) => void

        The action to perform

      Returns this

      The current Optional

    • 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<Exclude<R, null>>

    • Create an optional

      Type Parameters

      • T

      Parameters

      • value: T

      Returns Optional<Exclude<T, 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