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

    Function ensureNullableShape

    • Creates a shape from an object shape where any property that can be any kind of nullable (optional, null, or undefined) can now be all kinds of nullable. That is, any property that is optional or can be null or undefined becomes all three.

      Type Parameters

      • Original

      Parameters

      Returns Shape<TUnsafe<EnsureNullableType<ShapeInitType<Original>>>>

      import {
      nullToNullableShape,
      unionShape,
      assertValidShape,
      defineShape,
      } from 'object-shape-tester';

      const myShape = nullToNullableShape({
      a: '',
      b: unionShape(null, -1),
      c: null,
      d: {
      e: unionShape(null, ''),
      },
      });

      // passes
      assertValidShape({a: 'hi', b: null, d: {}}, myShape);
      assertValidShape({a: 'hi', b: undefined, d: {e: undefined}}, myShape);

      // fails
      assertValidShape({b: null}, myShape);
      assertValidShape({a: 'hi'}, myShape);