@binarymuse/ts-stdlib
    Preparing search index...

    Interface OptionMethods<T>

    All the methods available on Option<T>.

    interface OptionMethods<T> {
        and: <U>(other: Option<U>) => Option<U>;
        andThen: <U>(fn: (value: T) => Option<U>) => Option<U>;
        equals: (other: Option<T>) => boolean;
        expect: (msg: string) => T;
        filter: (fn: (value: T) => boolean) => Option<T>;
        flatten: () => Option<T>;
        isNone: () => boolean;
        isNoneOr: (fn: (value: T) => boolean) => boolean;
        isSome: () => boolean;
        isSomeAnd: (fn: (value: T) => boolean) => boolean;
        map: <U>(fn: (value: T) => U) => Option<U>;
        mapOr: <U>(defaultValue: U, fn: (value: T) => U) => Option<U>;
        mapOrElse: <U>(defaultFn: () => U, mapFn: (value: T) => U) => Option<U>;
        match: <U>(cases: { none: () => U; some: (value: T) => U }) => U;
        okOr: <E>(defaultError: E) => Result<T, E>;
        okOrElse: <E>(fn: () => E) => Result<T, E>;
        or: (other: Option<T>) => Option<T>;
        orElse: (fn: () => Option<T>) => Option<T>;
        strictEquals: (other: Option<T>) => boolean;
        unwrap: () => T;
        unwrapOr: <U>(defaultValue: T | U) => T | U;
        unwrapOrElse: <U>(fn: () => T | U) => T | U;
        xor: (other: Option<T>) => Option<T>;
    }

    Type Parameters

    • T

      The type of the wrapped value

    Index

    Equality Checks

    equals: (other: Option<T>) => boolean

    Returns true if both options are Some and their inner values are equal using the JavaScript == operator, or if both options are None. If the inner values are both Options or Results, then the comparison will be recursive using equals on the inner values.

    Type declaration

      • (other: Option<T>): boolean
      • Parameters

        • other: Option<T>

          The option to compare against.

        Returns boolean

        Whether the options are equal.

    const opt1 = Some(1);
    const opt2 = Some(1);
    const result = opt1.equals(opt2); // true

    const opt3 = Some(Ok(1));
    const opt4 = Some(Ok(1));
    const result2 = opt3.equals(opt4); // true

    const opt5 = Some(None);
    const opt6 = Some(None);
    const result3 = opt5.equals(opt6); // true
    match: <U>(cases: { none: () => U; some: (value: T) => U }) => U

    Returns the result of calling cases.some() with the inner value if the option is Some, otherwise returns the result of calling cases.none().

    Type declaration

      • <U>(cases: { none: () => U; some: (value: T) => U }): U
      • Type Parameters

        • U

          The type of the result.

        Parameters

        • cases: { none: () => U; some: (value: T) => U }

          An object containing functions to handle both Some and None cases.

        Returns U

        The result of calling the appropriate function.

    const opt = Some(1);
    const result = opt.match({
    some: (value) => value * 2,
    none: () => 0,
    });
    strictEquals: (other: Option<T>) => boolean

    Returns true if both options are Some and their inner values are equal using the JavaScript === operator, or if both options are None. If the inner values are both Options or Results, then the comparison will be recursive using strictEquals on the inner values.

    Type declaration

      • (other: Option<T>): boolean
      • Parameters

        Returns boolean

        Whether the options are strictly equal.

    const obj = {};
    const opt1 = Some(obj);
    const opt2 = Some(obj);
    const result = opt1.strictEquals(opt2); // true

    const opt3 = Some(None);
    const opt4 = Some(None);
    const result2 = opt3.strictEquals(opt4); // true

    Query Methods

    isNone: () => boolean

    Returns true if the value is a None.

    Type declaration

      • (): boolean
      • Returns boolean

        true if the value is a None, false otherwise.

    const opt = Some(1);
    const result = opt.isNone(); // false

    const none = None;
    const result2 = none.isNone(); // true
    isNoneOr: (fn: (value: T) => boolean) => boolean

    Returns true if the value is a None or the given function returns true.

    Type declaration

      • (fn: (value: T) => boolean): boolean
      • Parameters

        • fn: (value: T) => boolean

          A function that takes the wrapped value and returns a boolean.

        Returns boolean

        true if the value is a None or the given function returns true, false otherwise.

    const opt = Some(1);
    const result = opt.isNoneOr(value => value > 0); // true

    const none = None;
    const result2 = none.isNoneOr(value => value > 0); // true
    isSome: () => boolean

    Returns true if the value is a Some.

    Type declaration

      • (): boolean
      • Returns boolean

        true if the value is a Some, false otherwise.

    const opt = Some(1);
    const result = opt.isSome(); // true

    const none = None;
    const result2 = none.isSome(); // false
    isSomeAnd: (fn: (value: T) => boolean) => boolean

    Returns true if the value is a Some and the given function returns true.

    Type declaration

      • (fn: (value: T) => boolean): boolean
      • Parameters

        • fn: (value: T) => boolean

          A function that takes the wrapped value and returns a boolean.

        Returns boolean

        true if the value is a Some and the given function returns true, false otherwise.

    const opt = Some(1);
    const result = opt.isSomeAnd(value => value > 0); // true

    const none = None;
    const result2 = none.isSomeAnd(value => value > 0); // false

    Transform Methods

    and: <U>(other: Option<U>) => Option<U>

    Returns None if the source option is None, otherwise returns other.

    Type declaration

      • <U>(other: Option<U>): Option<U>
      • Type Parameters

        • U

          The type of the new wrapped value.

        Parameters

        • other: Option<U>

          The option to return if this option is Some.

        Returns Option<U>

        Either None or the provided option.

    const opt = Some(1);
    const result = opt.and(Some(2)); // Some(2)

    const none = None;
    const result2 = none.and(Some(2)); // None
    andThen: <U>(fn: (value: T) => Option<U>) => Option<U>

    Returns None if the source option is None, otherwise calls fn with the inner value and returns the result.

    Type declaration

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

        • U

          The type of the new wrapped value.

        Parameters

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

          A function that takes the wrapped value and returns a new option.

        Returns Option<U>

        The result of fn or None.

    const opt = Some(1);
    const result = opt.andThen(value => Some(value * 2)); // Some(2)

    const none = None;
    const result2 = none.andThen(value => Some(value * 2)); // None
    filter: (fn: (value: T) => boolean) => Option<T>

    Returns None if the option is None, otherwise calls fn with the inner value and returns:

    • Some<T> with the original wrapped value if fn returns true
    • None if fn returns false

    Type declaration

      • (fn: (value: T) => boolean): Option<T>
      • Parameters

        • fn: (value: T) => boolean

          A function that takes the wrapped value and returns a boolean.

        Returns Option<T>

        Either this option or None.

    const opt = Some(1);
    const result = opt.filter(value => value > 0); // Some(1)

    const opt2 = Some(1);
    const result2 = opt2.filter(value => value < 0); // None

    const none = None;
    const result3 = none.filter(value => value > 0); // None
    flatten: () => Option<T>

    Converts from Option<Option<T>> to Option<T>. Only one level of nesting is removed.

    Type declaration

    const opt = Some(Some(1));
    const result = opt.flatten(); // Some(1)

    const opt2 = Some(Some(Some(1)));
    const result2 = opt2.flatten(); // Some(Some(1))

    const none = None;
    const result3 = none.flatten(); // None
    map: <U>(fn: (value: T) => U) => Option<U>

    Returns an Option<U> by mapping the inner value of the source option with fn.

    Type declaration

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

        • U

          The type of the new wrapped value.

        Parameters

        • fn: (value: T) => U

          A function that takes the wrapped value and returns a new value.

        Returns Option<U>

        A new option containing the transformed value.

    const opt = Some(1);
    const result = opt.map(value => String(value)); // Some("1")

    const none = None;
    const result2 = none.map(value => String(value)); // None
    mapOr: <U>(defaultValue: U, fn: (value: T) => U) => Option<U>

    Returns an option wrapping the provided defaultValue if the option is None, or calls fn with the inner value and returns a new option wrapping its return value.

    Type declaration

      • <U>(defaultValue: U, fn: (value: T) => U): Option<U>
      • Type Parameters

        • U

          The type of the new wrapped value.

        Parameters

        • defaultValue: U

          The value to use if the option is None.

        • fn: (value: T) => U

          A function that takes the wrapped value and returns a new value.

        Returns Option<U>

    const opt = Some(1);
    const result = opt.mapOr("default", value => String(value)); // Some("1")

    const none = None;
    const result2 = none.mapOr("default", value => String(value)); // Some("default")
    mapOrElse: <U>(defaultFn: () => U, mapFn: (value: T) => U) => Option<U>

    Returns an option wrapping the return value of defaultFn if the option is None, or calls mapFn with the inner value and returns a new option wrapping its return value. This function is useful for providing a default value that is expensive to compute or has side effects.

    Type declaration

      • <U>(defaultFn: () => U, mapFn: (value: T) => U): Option<U>
      • Type Parameters

        • U

          The type of the new wrapped value.

        Parameters

        • defaultFn: () => U

          A function that returns the default value if the option is None.

        • mapFn: (value: T) => U

          A function that takes the wrapped value and returns a new value.

        Returns Option<U>

        A new option containing either the default or transformed value.

    const opt = Some(1);
    const result = opt.mapOrElse(() => "default", value => String(value)); // Some("1")

    const none = None;
    const result2 = none.mapOrElse(() => "default", value => String(value)); // Some("default")
    okOr: <E>(defaultError: E) => Result<T, E>

    Converts the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(defaultValue).

    Type declaration

      • <E>(defaultError: E): Result<T, E>
      • Type Parameters

        • E

          The type of the error value.

        Parameters

        • defaultError: E

          The error value to use if the option is None.

        Returns Result<T, E>

        A result containing either the wrapped value or the error.

    const opt = Some(1);
    const result = opt.okOr("default error"); // Ok(1)

    const none = None;
    const result2 = none.okOr("default error"); // Err("default error")
    okOrElse: <E>(fn: () => E) => Result<T, E>

    Converts the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(fn()).

    Type declaration

      • <E>(fn: () => E): Result<T, E>
      • Type Parameters

        • E

          The type of the error value.

        Parameters

        • fn: () => E

          A function that returns the error value if the option is None.

        Returns Result<T, E>

        A result containing either the wrapped value or the error.

    const opt = Some(1);
    const result = opt.okOrElse(() => "default error"); // Ok(1)

    const none = None;
    const result2 = none.okOrElse(() => "default error"); // Err("default error")
    or: (other: Option<T>) => Option<T>

    Returns the source option if it is Some, otherwise returns other.

    Type declaration

      • (other: Option<T>): Option<T>
      • Parameters

        • other: Option<T>

          The option to return if this option is None.

        Returns Option<T>

        Either this option or the provided option.

    const opt = Some(1);
    const result = opt.or(Some(2)); // Some(1)

    const none = None;
    const result2 = none.or(Some(2)); // Some(2)
    orElse: (fn: () => Option<T>) => Option<T>

    Returns the source option if it is Some, otherwise calls fn and returns the result. This function is useful for providing a default value that is expensive to compute or has side effects.

    Type declaration

      • (fn: () => Option<T>): Option<T>
      • Parameters

        • fn: () => Option<T>

          A function that returns a new option.

        Returns Option<T>

        Either this option or the result of fn.

    const opt = Some(1);
    const result = opt.orElse(() => Some(2)); // Some(1)

    const none = None;
    const result2 = none.orElse(() => Some(2)); // Some(2)
    xor: (other: Option<T>) => Option<T>

    Returns the source option or other if exactly one of them is Some, otherwise returns None.

    Type declaration

      • (other: Option<T>): Option<T>
      • Parameters

        • other: Option<T>

          The option to compare against.

        Returns Option<T>

        Either this option, the other option, or None.

    const opt1 = Some(1);
    const opt2 = Some(2);
    const result = opt1.xor(opt2); // None

    const none = None;
    const result2 = none.xor(Some(2)); // Some(2)

    Unwrap Methods

    expect: (msg: string) => T

    Returns the wrapped value. If the option is None, this will throw an error with the provided message.

    Type declaration

      • (msg: string): T
      • Parameters

        • msg: string

          The message to throw if the option is None.

        Returns T

        The wrapped value.

    const opt = Some(1);
    const result = opt.expect("Expected a value"); // 1

    const none = None;
    const result2 = none.expect("Expected a value"); // throws Error
    unwrap: () => T

    Returns the wrapped value. If the option is None, this will throw an error.

    Type declaration

      • (): T
      • Returns T

        The wrapped value.

    const opt = Some(1);
    const result = opt.unwrap(); // 1

    const none = None;
    const result2 = none.unwrap(); // throws Error
    unwrapOr: <U>(defaultValue: T | U) => T | U

    Returns the wrapped value. If the option is None, this will return the provided default value.

    Type declaration

      • <U>(defaultValue: T | U): T | U
      • Type Parameters

        • U

        Parameters

        • defaultValue: T | U

          The value to return if the option is None.

        Returns T | U

        The wrapped value.

    const opt = Some(1);
    const result = opt.unwrapOr(0); // 1

    const none = None;
    const result2 = none.unwrapOr(0); // 0
    unwrapOrElse: <U>(fn: () => T | U) => T | U

    Returns the wrapped value. If the option is None, this will call the provided function and return its result. This function is useful for providing a default value that is expensive to compute or has side effects.

    Type declaration

      • <U>(fn: () => T | U): T | U
      • Type Parameters

        • U

        Parameters

        • fn: () => T | U

          A function that returns the default value if the option is None.

        Returns T | U

        The wrapped value.

    const opt = Some(1);
    const result = opt.unwrapOrElse(() => 0); // 1

    const none = None;
    const result2 = none.unwrapOrElse(() => 0); // 0