I have a following zod example:
const objectSchema = z.object({
optional: z.string().optional(),
other: z.string(),
});
In typescript, optional field key?: string
will be converted to key?: string | undefined
. But it’s strictly different during the runtime check.
The previous one during the runtime check could be interpreted as the optional
must be string
if it exists, hence optional: undefined
is an invalid input.
So, is there any way I can do with zod that makes the undefined
an invalid input.
hence
objectSchema({ other: "other" }) //pass.
objectSchema({ optional: undefined, other: "other" }) // fail
objectSchema({ optional: "str", other: "other" }) // pass