Skip to main content

whenNothing

Callable

  • whenNothing<T, M>(x: T, f: () => M): [Nothing] extends [T] ? IsEqual<T, Nothing> extends true ? M : M | Just<T> : T

  • If the given Maybe is a Nothing, then return the result of the given function, otherwise return the given Maybe.

    Example

    whenNothing(Nothing, () => `foobar`)
    // => "foobar"

    whenNothing(0, () => `foobar`)
    // => 0

    Type parameters

    • T: unknown

      The maybe type.

    • M: unknown = T

      The mapped type.

    Parameters

    • x: T

      The Maybe value to map.

    • f: () => M

      The map function.

    Returns [Nothing] extends [T] ? IsEqual<T, Nothing> extends true ? M : M | Just<T> : T

    The mapped value if Nothing.