I’m using zod’s infer
utility type and the basic usage is:
const mySchema = z.object({});
type myType = z.infer<typeof mySchema>;
In my case, mySchema
may be undefined, however the infer
type does not allow undefined to be used as the generic type parameter:
const mySchema: z.ZodType<unknown> | undefined = undefined;
type myType = z.infer<typeof mySchema>;
// Type 'undefined' does not satisfy the constraint 'ZodType<any, any, any>'
How can I handle mySchema
possibly being undefined here, so that myType
is either undefined or the inferred type?
I thought this might work, but it doesnt.
const mySchema: z.ZodType<unknown> | undefined = undefined;
type myType = typeof mySchema extends undefined
? undefined
: z.infer<typeof mySchema>;