Class Option<T>

Represents an optional value that may or may not exist.

Type Parameters

  • T

    The type of the value.

Constructors

  • Type Parameters

    • T

    Parameters

    • value: null | T

    Returns Option<T>

Properties

value: null | T

Methods

  • Returns the option if the current option is Some, otherwise returns a new Option with null value.

    Type Parameters

    • U

      The type of the value contained in the option parameter.

    Parameters

    • option: Option<U>

      The option to be returned if the current option is Some.

    Returns Option<U>

    • The option if the current option is Some, otherwise a new Option with null value.
  • Applies the given function to the value contained in the Option if it is Some. If the Option is None, it returns a new Option with a null value.

    Type Parameters

    • U

    Parameters

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

      The function to apply to the value.

    Returns Option<null> | Option<U>

    An Option that contains the result of applying the function, or a new Option with a null value.

  • Throws an error with the specified message if the option is None. Otherwise, returns the value of the option.

    Parameters

    • message: string

      The error message to throw if the option is None.

    Returns T

    The value of the option if it is Some.

    Throws

    Error with the specified message if the option is None.

  • Filters the option based on the given predicate function. If the predicate returns true for the option's value, returns the current option. If the predicate returns false for the option's value, returns a new empty option.

    Parameters

    • predicate: ((value) => boolean)

      The predicate function to filter the option.

        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Option<null>

    The filtered option.

  • Checks if the Option is None.

    Returns boolean

    True if the Option is None, false otherwise.

  • Checks if the Option is in a "Some" state.

    Returns boolean

    True if the Option is in a "Some" state, false otherwise.

  • Checks if the Option is Some and satisfies the given predicate.

    Parameters

    • predicate: ((value) => boolean)

      The predicate function to test the value with.

        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns boolean

    true if the Option is Some and the value satisfies the predicate, false otherwise.

  • Applies a function to the value of the Option if it is Some, and returns a new Option with the result. If the Option is None, returns a new Option with a null value.

    Type Parameters

    • U

      The type of the value returned by the mapping function.

    Parameters

    • fn: ((value) => U)

      The mapping function to apply to the value.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Option<U>

    • A new Option with the mapped value.
  • Maps the value of the Option to a new value using the provided function, or returns a default value if the Option is None.

    Type Parameters

    • U

      The type of the new value.

    Parameters

    • defaultValue: U

      The default value to return if the Option is None.

    • fn: ((value) => U)

      The function to apply to the value of the Option.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

    • The mapped value or the default value.
  • Maps the value of the Option to a new value using the provided function, or returns a default value if the Option is None.

    Type Parameters

    • U

      The type of the new value.

    Parameters

    • defaultValue: (() => U)

      A function that returns the default value.

        • (): U
        • Returns U

    • fn: ((value) => U)

      A function that maps the value of the Option to a new value.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

    • The mapped value or the default value.
  • Returns this Option if it is Some, otherwise returns the provided Option.

    Parameters

    • option: Option<T>

      The Option to return if this Option is None.

    Returns Option<T>

    The current Option if it is Some, otherwise the provided Option.

  • Returns this Option if it is Some, otherwise evaluates the given function fn and returns its result.

    Parameters

    Returns Option<T>

    The current Option if it is Some, otherwise the result of evaluating the function fn.

  • Unwraps the value contained in the Option.

    Returns T

    The unwrapped value.

    Throws

    if the Option is None.

  • Returns the wrapped value if it exists, otherwise returns the provided default value.

    Parameters

    • defaultValue: T

      The default value to return if the option is None.

    Returns T

    The wrapped value if it exists, otherwise the default value.

  • Returns the wrapped value if it is Some, otherwise invokes the provided function fn and returns its result.

    Parameters

    • fn: (() => T)

      The function to be invoked when the wrapped value is None.

        • (): T
        • Returns T

    Returns T

    The wrapped value if it is Some, otherwise the result of invoking the provided function fn.

  • Returns a new Option that represents the exclusive or (XOR) operation between this Option and the provided Option. If one of the Options is Some and the other is None, it returns the Some Option. If both Options are Some or both are None, it returns a new None Option.

    Parameters

    • option: Option<T>

      The Option to perform the XOR operation with.

    Returns Option<null>

    A new Option representing the XOR operation between this Option and the provided Option.

  • Creates an Option instance from a value.

    Type Parameters

    • T

      The type of the value.

    Parameters

    • value: null | T

      The value to create an Option from.

    Returns Option<T>

    An Option instance.

  • Checks if a value is an instance of the Option class.

    Parameters

    • value: any

      The value to check.

    Returns boolean

    true if the value is an instance of Option, false otherwise.

  • Matches the given option against the provided patterns and returns the result.

    Type Parameters

    • T

      The type of the option value.

    • R

      The type of the result.

    Parameters

    • option: Option<T>

      The option to match against.

    • patterns: {
          None: (() => R);
          Some: ((value) => R);
      }

      The patterns to match.

      • None: (() => R)
          • (): R
          • Returns R

      • Some: ((value) => R)
          • (value): R
          • Parameters

            • value: T

            Returns R

    Returns R

    • The result of the matching pattern.
  • Represents an empty Option.

    Type Parameters

    • T

    Returns Option<T>

    An empty Option.

    Typeparam

    T The type of the value.

  • Creates an Option instance with a non-null value.

    Type Parameters

    • T

      The type of the value.

    Parameters

    • value: T

      The value to be wrapped in an Option.

    Returns Option<T>

    An Option instance containing the provided value.