8000 ZodObject that are `.refine` or `.superRefine` returns a ZodEffects type · Issue #2474 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
ZodObject that are .refine or .superRefine returns a ZodEffects type #2474
Open
@morganjbt

Description

@morganjbt

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0