@bodil/opt
    Preparing search index...

    Variable Option

    Option: {
        Class: typeof Option;
        from<A>(value: A | undefined): Option<A>;
        fromJSON<A>(doc: { result: boolean; value?: A }): Option<A>;
        is(value: unknown): value is Option<unknown>;
        lift<A, F extends (...args: any[]) => A | undefined>(
            fn: F,
        ): (...args: Parameters<F>) => Option<A>;
    }

    Static methods on the Option object.

    Type Declaration

    • Class: typeof Option

      The class constructor of Options. You should never use this to construct Options directly, preferring instead Some and None. It's exposed for use in instanceof checks, though calling Option.is to decide option-ness is preferred.

      import { expect } from "chai";
      expect(Some("pony")).to.be.an.instanceof(Option.Class);
    • from: function
      • Create an Option from a value that's either A or undefined.

        If the provided value is undefined, it will create a None. Otherwise, it will create a Some containing the value.

        Type Parameters

        • A

        Parameters

        • value: A | undefined

        Returns Option<A>

        // Guarded index lookup:
        function getIndex<A>(array: A[], index: number): Option<A> {
        return Option.from(array[index]);
        }
    • fromJSON: function
    • is: function
      • Test whether an unknown value is an Option.

        Parameters

        • value: unknown

        Returns value is Option<unknown>

        function assertOption(value: unknown): void {
        if (Option.is(value)) {
        value.assertSome();
        }
        }
    • lift: function
      • Turn a function that returns A | undefined into a function that returns Option<A>.

        Beware that TypeScript's type inference isn't currently very good at this, so you should explicitly provide the target function signature when using this function, or you're likely to end up with an Option<unknown> instead of the expected Option<A>.

        Type Parameters

        • A
        • F extends (...args: any[]) => A | undefined

        Parameters

        • fn: F

        Returns (...args: Parameters<F>) => Option<A>

        function nonNeg(n: number): number | undefined {
        return n >= 0 ? n : undefined;
        }
        const liftedNonNeg: (n: number) => Option<number> = Option.lift(nonNeg);