Class Option<T>Abstract

Represents an optional value (Some<T> for present values, None for absence). Implements a functional-style API for safe operations without null checks.

Type Parameters

  • T

    The type of the contained value.

Hierarchy (view full)

Constructors

Methods

  • Filters the Option<T> based on a predicate.

    Parameters

    • predicate: ((value: T) => boolean)

      The condition to check.

        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Option<T>

    • The original Option<T> if the condition is met, otherwise None.
  • Maps over the contained value but expects the function to return another Option<U>.

    Type Parameters

    • U

      The return type wrapped in an Option<U>.

    Parameters

    • fn: ((value: T) => Option<U>)

      A function returning an Option<U>.

    Returns Option<U>

    • The transformed Option<U>, or None if original was None.
  • Executes a function based on whether the Option is Some or None.

    Type Parameters

    • U

      The return type.

    Parameters

    • ifNone: (() => U)

      Function to call if None.

        • (): U
        • Returns U

    • ifSome: ((value: T) => U)

      Function to call with the value if Some.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

    • The result of calling ifNone or ifSome.
  • Provides a default value if the Option is None.

    Parameters

    • defaultValue: T

      The fallback value.

    Returns T

    • The contained value if present, otherwise defaultValue.
  • Transforms the contained value if present.

    Type Parameters

    • U

      The return type of the transformation function.

    Parameters

    • fn: ((value: T) => U)

      A function to apply to the value if present.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Option<U>

    • The transformed Option<U>, or None if original was None.
  • Provides an alternative Option<T> if the original is None.

    Parameters

    • alternative: Option<T>

      The alternative Option<T> to return if this is None.

    Returns Option<T>

    • The original if Some, otherwise alternative.
  • Creates an instance of Some<T> if a value is present, otherwise returns None.

    Type Parameters

    • T

    Parameters

    • value: T

      The value to wrap.

    Returns Option<T>

    • Some<T> if valid, otherwise None.