object-shape-tester - v6.3.3
    Preparing search index...

    Function optionalShape

    • Creates a shape that allows an object property to be missing.

      Type Parameters

      • T
      • const AlsoUndefined extends boolean = false

      Parameters

      • shape: T
      • options: PartialWithUndefined<{ alsoUndefined: AlsoUndefined }> = {}

      Returns Shape<
          TAddOptional<
              AlsoUndefined extends true
                  ? TUnion<[TUndefined, ShapeInitSchema<T>]>
                  : ShapeInitSchema<T>,
          >,
      >

      import {optionalShape, checkValidShape, defineShape} from 'object-shape-tester';

      const myShape = defineShape({
      a: '',
      b: optionalShape(-1),
      });

      checkValidShape({a: 'hi', b: 0}, myShape); // `true`
      checkValidShape({a: 'hi'}, myShape); // `true`
      checkValidShape({b: 0}, myShape); // `false`