Interface Option<T>

The Option interface representing an optional value. An Option is either Some, holding a value, or None, indicating the absence of a value.

interface Option<T> {
    type: symbol;
    and<U>(optb): Option<U>;
    andThen<U>(fn): Option<U>;
    isNone(): this is NoneOption<T>;
    isSome(): this is SomeOption<T>;
    map<U>(fn): Option<U>;
    match<U>(fn): U;
    or(optb): Option<T>;
    unwrap(): T;
    unwrapOr(def): T;
}

Type Parameters

  • T extends NonUndefined

Hierarchy (view full)

Properties

type: symbol

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

Methods

  • Returns the option provided as a parameter if the original Option is Some, otherwise returns None.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • optb: Option<U>

      The Option to return if the original Option is Some.

    Returns Option<U>

    optb if the original Option is Some, otherwise None.

    Examples

    const anotherOption = Some("another");
    const someOption = Some("some").and(anotherOption); // Some("another")
    const noneOption = None.and(anotherOption); // None
  • Transforms the Option into another by applying a function to the contained value, chaining multiple potentially failing operations.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: ((val) => Option<U>)

      A function that takes a value of type T and returns an Option of type U.

    Returns Option<U>

    The Option returned by the function if the original Option is Some, otherwise None.

    Examples

    const parse = (s: string) => {
    const parsed = parseInt(s);
    return isNaN(parsed) ? None : Some(parsed);
    };
    const result = Some("123").andThen(parse); // Some(123)
    const noResult = Some("abc").andThen(parse); // None
  • Determines if the Option is None.

    Returns this is NoneOption<T>

    true if the Option is None, otherwise false.

    Example

    console.log(Some(5).isNone()); // false
    console.log(None.isNone()); // true
  • Determines if the Option is a Some.

    Returns this is SomeOption<T>

    true if the Option is Some, otherwise false.

    Example

    console.log(Some(5).isSome()); // true
    console.log(None.isSome()); // false
  • Applies a function to the contained value (if any), or returns a default if None.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: ((val) => U)

      A function that takes a value of type T and returns a value of type U.

        • (val): U
        • Parameters

          • val: T

          Returns U

    Returns Option<U>

    An Option containing the function's return value if the original Option is Some, otherwise None.

    Examples

    const length = Some("hello").map(s => s.length); // Some(5)
    const noneLength = None.map(s => s.length); // None
  • 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 this Option if it is Some, otherwise returns the option provided as a parameter.

    Parameters

    • optb: Option<T>

      The alternative Option to return if the original Option is None.

    Returns Option<T>

    The original Option if it is Some, otherwise optb.

    Examples

    const defaultOption = Some("default");
    const someOption = Some("some").or(defaultOption); // Some("some")
    const noneOption = None.or(defaultOption); // Some("default")
  • Unwraps an Option, yielding the contained value if Some, otherwise throws an error.

    Returns T

    The contained value.

    Throws

    Error if the Option is None.

    Examples

    console.log(Some("value").unwrap()); // "value"
    console.log(None.unwrap()); // throws Error
  • 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"