Interface IOption<A>

Methods available on Option objects.

interface IOption<A> {
    and<B>(option: Option<B>): Option<B>;
    ap<B>(f: Option<((a: A) => B)>): Option<B>;
    assertNone(): asserts this is None;
    assertSome(): asserts this is Some<A>;
    chain<B>(f: ((value: A) => Option<B>)): Option<B>;
    chainNone(f: (() => Option<A>)): Option<A>;
    getOr(defaultValue: A): A;
    getOrElse(f: (() => A)): A;
    ifNone(onNone: (() => void)): void;
    ifSome(onSome: ((value: A) => void)): void;
    isNone(): this is None;
    isSome(): this is Some<A>;
    map<B>(f: ((value: A) => B)): Option<B>;
    match<B>(onSome: ((value: A) => B), onNone: (() => B)): B;
    okOr<E>(error: E): Result<A, E>;
    okOrElse<E>(f: (() => E)): Result<A, E>;
    or(option: Option<A>): Option<A>;
    toJSON(): {
        result: true;
        value: A;
    } | {
        result: false;
    };
    unwrap(): undefined | A;
    unwrapExact(message?: string): A;
}

Type Parameters

  • A

    The type of the contained value.

Methods

  • If the Option isn't empty, return the provided Option of B, otherwise return None.

    Type Parameters

    • B

    Parameters

    Returns Option<B>

  • Given another Option containing a function from A to B, if both Options are Some, call that function with the value of this Option and return an Option<B> containing the function's return value. If either Option is None, return None.

    Type Parameters

    • B

    Parameters

    Returns Option<B>

  • Assert that the Option is empty.

    Throws a TypeError if it isn't.

    Returns asserts this is None

  • Assert that the Option contains a value.

    Throws a TypeError if it doesn't.

    Returns asserts this is Some<A>

  • If the Option isn't empty, call the provided function with the contained value and return a new Option containing the result of the function, which must be another Option. If the Option is empty, return None without calling the function.

    This is the monadic bind function, for those who celebrate.

    Type Parameters

    • B

    Parameters

    Returns Option<B>

  • If the Option is empty, call the provided function and return its result, which must be another Option of A. If the Option is Some, return the Some value without calling the function.

    Parameters

    Returns Option<A>

  • Return the value contained in the Option if it's not empty, or return defaultValue otherwise.

    Parameters

    • defaultValue: A

    Returns A

  • Return the value contained in the Option if it's not empty, or call the provided function and return its result otherwise.

    Parameters

    • f: (() => A)
        • (): A
        • Returns A

    Returns A

  • Call the provided function if the Option is empty.

    Parameters

    • onNone: (() => void)
        • (): void
        • Returns void

    Returns void

  • Call the provided function with the contained value if the Option isn't empty.

    Parameters

    • onSome: ((value: A) => void)
        • (value): void
        • Parameters

          • value: A

          Returns void

    Returns void

  • Test if the Option is empty.

    Returns this is None

  • Test if the Option contains a value.

    Returns this is Some<A>

  • If the Option isn't empty, transform its contained value using the provided function.

    Type Parameters

    • B

    Parameters

    • f: ((value: A) => B)
        • (value): B
        • Parameters

          • value: A

          Returns B

    Returns Option<B>

  • The match function takes two callbacks, one for each possible state of the Option, and calls the one that matches the actual state.

    Type Parameters

    • B

    Parameters

    • onSome: ((value: A) => B)
        • (value): B
        • Parameters

          • value: A

          Returns B

    • onNone: (() => B)
        • (): B
        • Returns B

    Returns B

  • Convert the Option into a Result, using the provided error value if the Option is empty.

    Type Parameters

    • E

    Parameters

    • error: E

    Returns Result<A, E>

  • Convert the Option into a Result, calling the provided function to obtain an error value if the Option is empty.

    Type Parameters

    • E

    Parameters

    • f: (() => E)
        • (): E
        • Returns E

    Returns Result<A, E>

  • Convert an Option into a JSON structure for serialisation.

    Returns {
        result: true;
        value: A;
    } | {
        result: false;
    }

  • Convert the Option into an optional value of A.

    Returns undefined | A

  • Convert the Option into a value of A, throwing a TypeError when empty, optionally with the provided error message.

    Parameters

    • Optionalmessage: string

    Returns A