colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.72k stars 1.17k forks source link

z.infer<> fails to infer nested union type. #3703

Open grogugrogu opened 2 months ago

grogugrogu commented 2 months ago

Hey everyone, I seem to have run into an issue but I'm unsure if this is by design or there's a way around this. z.infer seems to skip an entire schema and only shows a schema of shared fields only for a nested field with a union type.

field: z
        .union([zod1Schema, zod2Schema])
        .nullable()
        .optional(),

The result is a type that only shows what's shared between the two schemas and ignores fields from zod2Schema.

powersjcb commented 2 months ago

I was having similar issues. Do we know if this a regression or has this always been this way?

I'm working on some repros for this outside of our prod codebase.