zod: Zod 3.22.2 TS 5.2.2: TS2589: Type instantiation is excessively deep and possibly infinite
const EventSchema = z.union([
z.object({
action: z.enum(['create', 'drop', 'update', 'hydrate']),
}),
z.object({
action: z.literal('rollback'),
tag: z.string().optional().default('initialState'),
}),
]);
Error: src/handler.ts(64,3): error TS2589: Type instantiation is excessively deep and possibly infinite
Related (both closed): #495 and #577
Tried rimraf node modules and reinstalling. That type seems so simple that I don’t see what I can do to simplify it.
Is there a playground that I can pull Zod into to reproduce it?
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 18
- Comments: 19 (2 by maintainers)
Commits related to this issue
- Downgrade Zod to 3.21.4 (see https://github.com/colinhacks/zod/issues/2697 ). — committed to vercel/modelfusion by lgrammel 3 years ago
- Downgrade Zod to 3.21.4 (see https://github.com/colinhacks/zod/issues/2697 ). — committed to vercel/modelfusion by lgrammel 3 years ago
I found out that this issue may occur when multiple versions of
zodpackage is installed.UPD: To be clear, run the following (or similar) command in order to find out which versions of Zod (including transitive dependencies) you have installed:
if there is more than one, then it might be the cause. I won’t advise on particular approach on deduplicating it, since it may depend on a particular situation.
I had the same situation in turbo repo. I solved the problem with the steps I applied. In the same way, if you do it one by one, I think it will be a solution for you.
pnpm list zodAfter listing, you can see which zod version is in which project. The error is usually caused by different versions in different projects. Choose a common version according to the version that suits you and edit the package.json files.pnpm prune && pnpm store pruneclean the cachepnpm installAfter installation, you will see that the problem is solved.+1 on @RobinTail 's answer. Im on Turborepo and had inconsistent Zod versions across the packages and apps. Syncing them fixed it. Thanks.
Well, it happened to me too in a monolithic repo where I had two different versions:
3.23.7and3.23.8. I synced both dependencies to the latest version, and it works 🤝🏻We have the same issue. Reverting back to the previously used version (3.21.4) solves the issue for us.
I have double-checked and we have consistent versions of zod in our project.
We keep getting “Type instantiation is excessively deep and possibly infinite” where we are 100% sure it is not
where phoneContactSchema looks like:
It makes tsc run terribly slow which is doubly annoying…
EDITED: watch out transitive dependencies too… a patch version difference (3.24.1 and 3.24.2) was making a difference. With due respect, this isn’t great…
My 2¢:
To keep zod version in sync in a pnpm monorepo, setup a catalog:
Then refer to it in
pacakage.jsonfiles:Edit: turns out you can’t use
catalog:in overrides, or you can but it will override the catalog too.And if an external lib was causing the version mismatch issue, you can refer to
catalog:in rootpackage.jsonoverrides too:Renovate and
pnpm updatesupport catalogs nowadays.We were struggling with this along with use along with TRPC because we were returning a JsonValue from our procedure. Just throwing it out there in case this helps anyone.
As far as I’ve tried; it seems like this error only occurs when unions are inside objects. @JacobWeisenburger Here’s a reproducible code example (This is running on latest Zod 3.22.4, with Typescript 5.3.3, but it also occurs with Zod 3.21.2 too)
@JacobWeisenburger I think @ericallam provided a good example.
Currently, we are not able to upgrade to 3.22.0 or 3.22.4 as we get the same exception.
Do you have any plans on working on this or does anyone have some hints, so I can try to debug it myself / create a PR?
Edit: Other then @ericallam, we were able to upgrade our underlying library from zod 3.21.4 to 3.22.4 as we do not have these constraints. Now it works, so this has something to do with using different zod versions.
I had this same issue on 3.22.2 but upgrading to 3.22.4 fixed it for me
Saved my day! I my case I forgot to add a directory to yarn workspaces, which resulted in different zod deps in app/package 🫠
@kadirmetin your solution worked for me, thanks for writing that up!
I have problems with this as well (typescript: 5.2.2, zod 3.22.4). Things will build fine to begin with in watch mode. But whenever I open a file that has the problematic type definition, it will start failing until I restart the build.