Open
Description
Zod version 3.21.4
Problem
Using .refine()
or .superRefine()
on ZodObject
(z.object()
) prevents the use of .shape
or .merge()
method as the returned type is a ZodEffects
and not a ZodObject
.
Example
const schema1 = z.object({ foo: z.string() }) // = type ZodObject
const schema2 = z.object({
bar: z.string(),
attributeRelatedToBar: z.string(),
}).superRefine(handleSchema2Errors) // = type ZodEffects
/**
* Impossible because `.merge` expect `schema2` to be type of `ZodObject` instead of `ZodEffects`:
* TS2352: Conversion of type 'ZodEffects […] to type 'ZodObject ' may be a mistake because
* neither type sufficiently overlaps with the other.
* If this was intentional, convert the expression to 'unknown' first.
*/
const finalSchema = schema1.merge(schema2)
/**
* Same error with `.shape` that expect a `ZodObject` instead of `ZodEffects`:
* TS2339: Property 'shape' does not exist on type 'ZodEffects'.
*/
schema2.shape.bar
schema2.shape.attributeRelatedToBar
Expected behavior
Using .superRefine()
or .refine()
on z.object()
should return a ZodObject
type
– or –
.merge()
& .shape
should be functional on ZodEffects
applied to a ZodObject
.