Interface SomeOption<T>

Implementation of Option representing a value (Some).

interface SomeOption<T> {
    type: symbol;
    and<U>(optb: SomeOption<U>): SomeOption<U>;
    and<U>(optb: NoneOption<U>): NoneOption<U>;
    andThen<U>(fn: ((val: T) => SomeOption<U>)): SomeOption<U>;
    andThen<U>(fn: ((val: T) => NoneOption<U>)): NoneOption<U>;
    andThen<U>(fn: ((val: T) => Option<U>)): Option<U>;
    isNone(): this is NoneOption<T>;
    isSome(): this is SomeOption<T>;
    map<U>(fn: ((val: T) => U)): SomeOption<U>;
    match<U>(fn: Match<T, U>): U;
    or<U>(_optb: Option<U>): SomeOption<T>;
    unwrap(): T;
    unwrapOr(def: T): 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: SomeOption<U>

      The Option to return if the original Option is Some.

    Returns SomeOption<U>

    optb if the original Option is Some, otherwise None.

    const anotherOption = Some("another");
    const someOption = Some("some").and(anotherOption); // Some("another")
    const noneOption = None.and(anotherOption); // None
  • Returns the option provided as a parameter if the original Option is Some, otherwise returns None.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • optb: NoneOption<U>

      The Option to return if the original Option is Some.

    Returns NoneOption<U>

    optb if the original Option is Some, otherwise None.

    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

    Returns SomeOption<U>

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

    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
  • Transforms the Option into another by applying a function to the contained value, chaining multiple potentially failing operations.

    Type Parameters

    • U extends NonUndefined

    Parameters

    Returns NoneOption<U>

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

    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
  • 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: T) => 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.

    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.

    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.

    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: T) => 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 SomeOption<U>

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

    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 void | 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.

    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.

    Type Parameters

    • U extends NonUndefined

    Parameters

    Returns SomeOption<T>

    The original Option if it is Some, otherwise optb.

    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.

    Error if the Option is None.

    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.

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