Skip to content

v4: Regression in generic type inference for ZodType compared to ZodSchema #4492

Description

@mn-prp

In v4, ZodSchema is deprecated and aliased to ZodType, but the type inference powers of the latter are much more limited.

The following is a pattern we use in many places in our code at my workplace:

import { z as v3 } from 'zod/v3'
import { z as v4 } from 'zod/v4'

const schemaV3 = v3.object({
  foo: v3.array(v3.string()),
  bar: v3.literal('baz')
})

const genericV3 = <In, Out>(schema: v3.ZodSchema<In,v3.ZodTypeDef, Out>): { in: In; out: Out } => {
  return undefined as any
}


/* Correctly inferred as
  {
    in: {
        foo: string[];
        bar: "baz";
    };
    out: {
        foo: string[];
        bar: "baz";
    };
  }
 */
const testV3 = genericV3(schemaV3) 

const schemaV4 = v4.object({
  foo: v4.array(v4.string()),
  bar: v4.literal('baz')
})

const genericV4 = <In, Out>(schema: v4.ZodType<In, Out>): { in: In; out: Out } => {
  return undefined as any
}

/* Incorrectly inferred as
{ in: any; out: any; }
 */
const testV4 = genericV4(schemaV4)

Is this a temporary regression? Is there an alternate typing we should be using that will accomplish the goal of writing generic functions and classes over the types implied by a zod schema?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions