8000 "Type instantiation is excessively deep and possibly infinite" when using Zod with React Hook Form and @hookform/resolvers · Issue #3987 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

"Type instantiation is excessively deep and possibly infinite" when using Zod with React Hook Form and @hookform/resolvers #3987

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

Open
jaus14 opened this issue Feb 14, 2025 · 16 comments

Comments

@jaus14
Copy link
jaus14 commented Feb 14, 2025

When using zod, react-hook-form, and @hookform/resolvers, I encountered a TypeScript error:

Type instantiation is excessively deep and possibly infinite.

This happens when defining a schema with Zod and passing it to @hookform/resolvers/zod.

Steps to Reproduce

  1. Define a schema:
    import { z } from "zod";
    
    const FormSchema = z.object({
      name: z.string(),
      loginId: z.string(),
      password: z.string(),
    })
  2. Use it in useForm:
    import { useForm } from "react-hook-form";
    import { zodResolver } from "@hookform/resolvers/zod";
    
    const form = useFormz.infer<typeof FormSchema>>({
      resolver: zodResolver(schema),
    });
  3. TypeScript throws an error:
    Type instantiation is excessively deep and possibly infinite.
    
Image Image

I fix it by downgrading Zod

  • Downgrading Zod from 3.42.2 to 3.42.1 resolved the issue.

Environment

  • TypeScript: 5.3.3
  • Zod: 3.42.2
  • React Hook Form: 7.54.2
  • @hookform/resolvers: 3.10.0
@paleite
Copy link
paleite commented Feb 14, 2025

Running into the exact same issue

@antonius-dev
Copy link

I can confirm, the problems start yesterday, after upgrading from 3.24.1 to 3.24.2, downgrading solves the problem!

@VitAndrGuid
Copy link

@antonius-dev Thanks !

@0xPBIT
Copy link
0xPBIT commented Feb 22, 2025

Confirmed as well. Downgrading patch solved the issue for me.

@njacob1001
Copy link

Same issue!

@taylor-lindores-reeves
Copy link

In Turborepo we have a shared package which exports Schemas. Using the zodResolver(schema) I received an error "Type instantiation is excessively deep and possibly infinite" - Fixed by downgrading from zod@3.24.3 to zod@3.24.2 and restart TS server.

@TasseDeCafe
Copy link

@taylor-lindores-reeves thanks. Unfortunately the most minor patches often cause issues like that with zod.

@kongsicheng
Copy link

Thanks. The problem occurs when I use version 3.24.3.

@yleflour
Copy link

If you use yarn you can use yarn why zod to investigate.
I found out that my issue was a mismatch between zod versions accross dependencies.
I had to add "resolutions": {"zod": "<version_number>"} to my package.json to fix this.

My take is that external libraries should always be using zod as a peerDependency to avoid such conflicts.

@leonahold
Copy link
leonahold commented May 10, 2025

I have been using the zod version 3.23.8 and react-hook-form 7.51.5 for a while to not have this problem. However, when we attempt to update to nextjs 15 and react 19, we HAVE to update react-hook-form and thus run into this problem again.

Just like the simple schema of the OP, my schema is just:

const FormSchema = z.object({
  items: z.array(z.string()),
});

Which shouldn't be excessively deep and possibly infinite. Is there really no way to tell react hook form that he should just swallow this message as a warning?

@qizf7
Copy link
qizf7 commented May 12, 2025

Running into the exact same issue

@qizf7
Copy link
qizf7 commented May 13, 2025

If you use yarn you can use yarn why zod to investigate. I found out that my issue was a mismatch between zod versions accross dependencies. I had to add "resolutions": {"zod": "<version_number>"} to my package.json to fix this.

My take is that external libraries should always be using zod as a peerDependency to avoid such conflicts.

Thank you! @yleflour This solution works for me!

@healqq
Copy link
healqq commented May 14, 2025

In our case it was a conflict of versions (we had 3.42.3 pinned but some eslint dependency brought 3.42.4). Bumping pinned version to 3.24.4 resolved the issue).

In case of pnpm running pnpm why zod will show what versions of zod are present.

@logemann
Copy link

also fixed it by downgrading to 3.24.2. No nerves to investigate more at this point.

@qcasey
Copy link
qcasey commented May 22, 2025

For me, downgrading to @hookform/resolvers@4.1.3 did the trick

@QuocZuong
Copy link

This works for me:

  • Zod: 3.24.2
  • React Hook Form: 7.54.2
  • @hookform/resolvers: 4.1.3

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

0