Variable Option

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

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: undefined | A

      Returns Option<A>

      IOption.unwrap

      // Guarded index lookup:
      function getIndex<A>(array: A[], index: number): Option<A> {
      return Option.from(array[index]);
      }
  • fromJSON:function
    • Create an Option from the output of IOption.toJSON.

      Type Parameters

      • A_2

      Parameters

      • doc: {
            result: boolean;
            value?: A_2;
        }
        • result: boolean
        • Optionalvalue?: A_2

      Returns Option<A_2>

  • 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_1
      • F extends ((...args: any[]) => undefined | A_1)

      Parameters

      • fn: F

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

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