Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
chida09 opened this issue Feb 26, 2025 · 1 comment
Closed

Comments

@chida09
Copy link

chida09 commented Feb 26, 2025

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.

@chida09 chida09 changed the title Type error when using zodResolver with z.coerce transformations Type error when using zodResolver with z.coerce transformations since v4.1.0 Feb 26, 2025
@jorisre
Copy link
Member

jorisre commented Feb 26, 2025

Thanks for the report! Closing this as a duplicate of #743

@jorisre jorisre closed this as completed Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants