Interface NoneOption<T>

Implementation of Option representing the absence of a value (None).

interface NoneOption<T> {
    type: symbol;
    isNone(): this is NoneOption<T>;
    isSome(): this is SomeOption<T>;
    match<U>(fn): U;
    unwrapOr(def): T;
}

Type Parameters

  • T extends NonUndefined

Hierarchy (view full)

Properties

Methods

Properties

type: symbol

Represents the type of the Option: either Some or None. Useful for debugging and runtime checks.

Methods

  • Performs a match operation on the Option, allowing for branching logic based on its state. This method takes an object with functions for each case (Some or None) and executes the corresponding function based on the Option's state, returning the result.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: Match<T, U>

      An object containing two properties: some and none, which are functions to handle the Some and None cases, respectively.

    Returns U

    The result of applying the corresponding function based on the Option's state.

    Example

    const optionSome = Some(5);
    const matchResultSome = optionSome.match({
    some: (value) => `The value is ${value}.`,
    none: () => 'There is no value.',
    });
    console.log(matchResultSome); // Outputs: "The value is 5."

    const optionNone = None;
    const matchResultNone = optionNone.match({
    some: (value) => `The value is ${value}.`,
    none: () => 'There is no value.',
    });
    console.log(matchResultNone); // Outputs: "There is no value."
  • Returns the contained value if Some, otherwise returns the provided default value.

    Parameters

    • def: T

      The default value to return if the Option is None.

    Returns T

    The contained value if Some, otherwise def.

    Examples

    const someValue = Some("value").unwrapOr("default"); // "value"
    const noneValue = None.unwrapOr("default"); // "default"