zod icon indicating copy to clipboard operation
zod copied to clipboard

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

Open jcollum-nutrien opened this issue 1 year ago • 13 comments

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?

jcollum-nutrien avatar Aug 31 '23 17:08 jcollum-nutrien

Works just fine for me...

https://stackblitz.com/edit/stackblitz-starters-rh62xb?file=src%2Findex.ts

amirs18 avatar Sep 03 '23 00:09 amirs18

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

kasleet avatar Sep 12 '23 12:09 kasleet

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

JacobWeisenburger avatar Sep 22 '23 19:09 JacobWeisenburger

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

ericallam avatar Sep 27 '23 09:09 ericallam

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

peterferguson avatar Nov 20 '23 11:11 peterferguson

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

kasleet avatar Dec 04 '23 17:12 kasleet

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.

WilliamIPark avatar Jan 12 '24 17:01 WilliamIPark

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

swittk avatar Feb 08 '24 22:02 swittk

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.

lundmikkel avatar Feb 23 '24 10:02 lundmikkel

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

RobinTail avatar Apr 19 '24 19:04 RobinTail

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

hg-gruppo avatar May 10 '24 12:05 hg-gruppo

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 (https://github.com/colinhacks/zod/issues/2962#issuecomment-2104525898). May be worth mentioning in the documentation that having different version of Zod may be problematic (until it's fixed ^^).

loicknuchel avatar May 10 '24 12:05 loicknuchel

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

ann0nip avatar Jun 10 '24 23:06 ann0nip