@bodil/opt
    Preparing search index...

    Interface Option<A>

    A nullable value of type A.

    Option<A> is essentially the same as A | undefined, but with a lot of useful methods attached, and with the ability to distinguish between a missing value and a present value that's undefined.

    You can construct an Option using either the Some function to wrap a present value of A, or the value None to signify a null value. You can also convert a value that's A | undefined into an Option by calling Option.from.

    interface Option<A> {
        value: A;
        and<B>(option: Option<B>): Option<B>;
        ap<B>(f: Option<(a: A) => B>): Option<B>;
        assertNone(): void;
        assertSome(): void;
        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 OptionNone;
        isSome(): this is OptionSome<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: false } | { result: true; value: A };
        unwrap(): A | undefined;
        unwrapExact(message?: string): A;
    }

    Type Parameters

    • A

      The type of the contained value.

    Properties

    value: A

    The contained value of type A, if Option.isSome() is true. If Option.isNone() is true, there's no value and the type is never.

    Methods

    • If the Option isn't empty, call the provided function with the contained value and return 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>

    • 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

      Returns A

    • Call the provided function if the Option is empty.

      Parameters

      • onNone: () => void

      Returns void

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

      Parameters

      • onSome: (value: A) => void

      Returns void

    • Test if the Option is empty.

      Returns this is OptionNone

    • Test if the Option contains a value.

      Returns this is OptionSome<A>

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

      Type Parameters

      • B

      Parameters

      • f: (value: A) => 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
      • onNone: () => B

      Returns B

    • Convert an Option into a JSON structure for serialisation.

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

    • Convert the Option into an optional value of A.

      If the Option is None, this method returns undefined. If the Option is Some, it returns a value of A. Note that this value could also be undefined.

      Returns A | undefined

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

      Parameters

      • Optionalmessage: string

      Returns A