Represents a container that may or may not contain a value of type T. Maybe

Example: Using maybe to check a environment variable

const port = Maybe.create<string>(process.env.PORT)
.map(value => Number(value))
.map(value => Number.isNaN(value) ? 3000 : value)
.mapEmpty(() => 3000)
.unwrap()

Type Parameters

  • T

    The type of the value.

Constructors

Properties

Methods

Constructors

  • Private

    Private constructor to create an instance of Maybe.

    Type Parameters

    • T

    Parameters

    • value: undefined | null | T

      The value or absence of a value.

    Returns Maybe<T>

Properties

value: undefined | null | T

The value or absence of a value.

Methods

  • Flattens a Maybe instance by removing one level of nesting, if applicable. If the internal value is a Maybe instance, returns that instance directly; otherwise, returns the original Maybe instance.

    Returns Maybe<MaybeValueType<T>>

    A new Maybe instance with one level of nesting removed.

  • Maps the wrapped value of Maybe using a provided function and then flattens the result.

    Type Parameters

    • R

      The type of the result after applying the function.

    Parameters

    • fn: ((value) => R)

      The mapping function to apply to the internal value.

    Returns Maybe<MaybeValueType<R>>

    A new Maybe instance containing the result of applying the function.

  • Applies a function to the value inside the Maybe instance if it is present, returning a new Maybe instance containing the result of the function. If the Maybe instance is empty, it returns itself.

    Type Parameters

    • R

      The type of the result.

    Parameters

    • fn: ((value) => R)

      The function to apply to the value.

        • (value): R
        • Parameters

          • value: T

          Returns R

    Returns Maybe<R>

    A new Maybe instance containing the result of the function.

  • Applies a function to the value inside the Maybe instance if it is empty, returning a new Maybe instance containing the result of the function. If the Maybe instance is not empty, it returns itself.

    Type Parameters

    • R

      The type of the result.

    Parameters

    • fn: (() => R)

      The function to apply when the value is empty.

        • (): R
        • Returns R

    Returns Maybe<R>

    A new Maybe instance containing the result of the function.

  • Unwraps the value inside the Maybe instance.

    Returns undefined | null | T

    The value or absence of a value.

  • Static method to create an instance of Maybe.

    Type Parameters

    • T

      The type of the value.

    Parameters

    • value: undefined | null | T

      The value or absence of a value.

    Returns Maybe<T>

    An instance of Maybe.

Generated using TypeDoc