Skip to main content

asMaybe

Callable

  • asMaybe<T, N>(x: T, nothingValue?: N): [N] extends [T] ? IsEqual<T, N> extends true ? Nothing : Maybe<Exclude<T, N>> : Exclude<T, N>

  • Creates a Maybe from the given value.

    Example

    asMaybe("foobar")
    // => "foobar"

    asMaybe("foobar", "foobar")
    // => Nothing

    asMaybe("foobar", "barfoo")
    // => "foobar"

    Type parameters

    • T

      The Maybe type.

    • N = undefined

    Parameters

    • x: T

      The value to unpack.

    • optionalnothingValue: N

      The value to use as Nothing.

    Returns [N] extends [T] ? IsEqual<T, N> extends true ? Nothing : Maybe<Exclude<T, N>> : Exclude<T, N>

    The maybe value.