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

Most upvoted comments

I found out that this issue may occur when multiple versions of zod package 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:

yarn why zod

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.

  1. pnpm list zod After 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.
  2. pnpm prune && pnpm store prune clean the cache
  3. Clean all node_modules folders in the project.
  4. pnpm install After 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.7 and 3.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

src/components/ui/Wizard/StepSenderDetails.tsx:11:33 - error TS2589: Type instantiation is excessively deep and possibly infinite.

11 const StepSenderDetailsSchema = z.object({
                                   ~~~~~~~~~~
12   senderDetails: phoneContactSchema
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 });
   ~~

where phoneContactSchema looks like:

export const phoneContactSchema = z.object({
  type: z.literal('phone'),
  identifier: z.string().brand('PhoneNumber')
});

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:

# pnpm-workspace.yaml
catalog:
  zod: 3.25.63

Then refer to it in pacakage.json files:

{
  "dependencies": {
    "zod": "catalog:"
  }
}

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 root package.json overrides too:

{
  "pnpm": {
    "overrides": {
      "zod": "catalog:"
    }
  }
}

Renovate and pnpm update support 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)

const un = z.union([
  z.object({ h: z.string() }),
  z.number(),
  z.string(),
  z.symbol(),
]);
const un1 = z.object({
  u: un,
});
const un2 = z.array(un);
const un3 = z.object({
  u: un,
  uarr: un2,
});

// Type checks
/*
(OK)
type unt = string | number | symbol | {
    h: string;
}
*/
type unt = z.infer<typeof un>;
/*
(Not OK. Union inside object)
type unt1 = {
    u: (string | number | symbol | {
        h: string;
    }) & (string | number | symbol | {
        h: string;
    } | undefined);
}
*/
type unt1 = z.infer<typeof un1>;
/*
(OK. Union inside array)
type unt2 = (string | number | symbol | {
    h: string;
})[]
*/
type unt2 = z.infer<typeof un2>;
/*
(Not OK, Union inside object)
type unt3 = {
    u: (string | number | symbol | {
        h: string;
    }) & (string | number | symbol | {
        h: string;
    } | undefined);
    uarr: (string | number | symbol | {
        h: string;
    })[];
}
*/
type unt3 = z.infer<typeof un3>;

@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

I found out that this issue may occur when multiple versions of zod package 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:

yarn why zod

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.

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.