zod: "Type instantiation is excessively deep and possibly infinite" when using Zod with React Hook Form and @hookform/resolvers
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
- Define a schema:
import { z } from "zod"; const FormSchema = z.object({ name: z.string(), loginId: z.string(), password: z.string(), }) - 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), }); - TypeScript throws an error:
Type instantiation is excessively deep and possibly infinite.
I fix it by downgrading Zod
- Downgrading Zod from
3.42.2to3.42.1resolved the issue.
Environment
- TypeScript: 5.3.3
- Zod: 3.42.2
- React Hook Form: 7.54.2
- @hookform/resolvers: 3.10.0
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 32
- Comments: 20
Commits related to this issue
- chore: pin zod to 3.42.1 https://github.com/colinhacks/zod/issues/3987 — committed to danbillson/pouring-at by danbillson 2 years ago
I can confirm, the problems start yesterday, after upgrading from 3.24.1 to 3.24.2, downgrading solves the problem!
Running into the exact same issue
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 fromzod@3.24.3tozod@3.24.2and restart TS server.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 zodwill show what versions of zod are present.Confirmed as well. Downgrading patch solved the issue for me.
This works for me:
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:
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?
If you use yarn you can use
yarn why zodto 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.
@antonius-dev Thanks !