Function exactOptional

  • A struct which allows the property of an object to be absent, or to be present as long as it's valid and not set to undefined.

    This struct should be used in conjunction with the object from this library, to get proper type inference.

    Deprecated

    Use exactOptional and object from @metamask/superstruct@>=3.2.0 instead.

    Returns

    A struct to check if the given value is valid, or not present.

    Example

    const struct = object({
    foo: exactOptional(string()),
    bar: exactOptional(number()),
    baz: optional(boolean()),
    qux: unknown(),
    });

    type Type = Infer<typeof struct>;
    // Type is equivalent to:
    // {
    // foo?: string;
    // bar?: number;
    // baz?: boolean | undefined;
    // qux: unknown;
    // }

    Type Parameters

    • Type

    • Schema

    Parameters

    • struct: Struct<Type, Schema>

      The struct to check the value against, if present.

    Returns Struct<Type & ExactOptionalGuard, Schema>