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

Zod 3.22.2 TS 5.2.2: TS2589: Type instantiation is excessively deep and possibly infinite #2697

Open
jcollum-nutrien opened this issue Aug 31, 2023 · 14 comments

Comments

@jcollum-nutrien
Copy link

jcollum-nutrien commented Aug 31, 2023

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?

@amirs18
Copy link

amirs18 commented Sep 3, 2023

@kasleet
Copy link

kasleet commented Sep 12, 2023

We have the same issue. Reverting back to the previously used version (3.21.4) solves the issue for us.

@JacobWeisenburger
Copy link
Contributor

@jcollum-nutrien or @kasleet,
Please send a full reproducible code example.

@JacobWeisenburger JacobWeisenburger added the closeable? This might be ready for closing label Sep 22, 2023
@ericallam
Copy link

@JacobWeisenburger

I've created a reproduction repo: https://github.com/ericallam/zod-3-22-2-type-instantiation

I understand that there shouldn't be an expectation for this to work (mixing minor Zod updates) but we're (trigger.dev) running into a bunch of users who are trying to use us with 3.22.2 and not being able to. We could upgrade our packages to 3.22.2 but then 3.21.4 users are forced to upgrade (and there are still more 3.21.4 downloads in the last 7 days than 3.22.2)

@peterferguson
Copy link

I had this same issue on 3.22.2 but upgrading to 3.22.4 fixed it for me

@kasleet
Copy link

kasleet commented Dec 4, 2023

@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.

@JacobWeisenburger JacobWeisenburger removed the closeable? This might be ready for closing label Dec 4, 2023
@WilliamIPark
Copy link

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.

@swittk
Copy link

swittk commented Feb 8, 2024

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>;

@lundmikkel
Copy link

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.

@RobinTail
Copy link
Contributor

I found out that this issue may occur when multiple versions of zod package is installed.

@hg-gruppo
Copy link

Had the same issue with using zod in two different projects. After aligning their versions, the issue disappeared.

@loicknuchel
Copy link

Same here, having version 3.23.8 of Zod and a 3.23.6 one on a dependency.
Moved both on 3.23.8 and no issue anymore.
I also had an out of memory issue due to that (#2962 (comment)).
May be worth mentioning in the documentation that having different version of Zod may be problematic (until it's fixed ^^).

@ann0nip
Copy link

ann0nip commented Jun 10, 2024

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 🤝🏻

@valajczech
Copy link

+1 on @RobinTail 's answer. Im on Turborepo and had inconsistent Zod versions across the packages and apps. Syncing them fixed it. Thanks.

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