Skip to content

Type error when using zodResolver with z.coerce transformations since v4.1.0 #751

@chida09

Description

@chida09

Describe the bug
When using zodResolver with a schema that includes type transformations (like z.coerce.number()), TypeScript shows a type error due to incompatibility between the input type and the transformed output type.

To Reproduce
Steps to reproduce the behavior:

  1. Create a form with string inputs that should be transformed to numbers
  2. Define a Zod schema using z.coerce.number()
  3. Set up a useForm hook with proper generics for input and output types
  4. Use zodResolver(schema) as the resolver
  5. Observe TypeScript error about incompatible types

Codesandbox link (Required)
https://codesandbox.io/p/sandbox/ns3wp2?file=%2Fsrc%2FApp.tsx

Expected behavior
The zodResolver should properly handle the type transformation from the input type to the output type without TypeScript errors. It should recognize that the input type (string) will be transformed to the output type (number) according to the Zod schema's coercion rules.

Screenshots

Image

Image

Additional context
This issue seems to have been introduced by PR #739
While this PR aimed to improve type inference for resolvers, it appears to have created a new problem when using z.coerce transformations with different input and output types. Before this change, the types were more flexible, but now the strict type checking causes errors when form values (strings) are coerced to different types (numbers) by the Zod schema.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions