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 doesn't work with typescript 4.3.2 #473

Closed
KATT opened this issue May 26, 2021 · 6 comments
Closed

zod doesn't work with typescript 4.3.2 #473

KATT opened this issue May 26, 2021 · 6 comments

Comments

@KATT
Copy link
Contributor

KATT commented May 26, 2021

see #472

@prueker
Copy link

prueker commented May 26, 2021

Ran into the same issue today when updating packages.

Keeping typescript at 4.2.4 works for now as a workaround.

@KATT KATT changed the title zod seems doesn't seem to work with typescript `4.3.2 zod seems doesn't seem to work with typescript 4.3.2 May 26, 2021
@toresbe
Copy link

toresbe commented May 27, 2021

Thank you for filing this issue and giving a workaround which saved us some tzores.

Can confirm that pinning TS to 4.2.3 solved our problem.

For anyone Googling, this is the error we got:

Excessive stack depth comparing types 'ZodError<?>' and 'ZodError<?>

@KATT
Copy link
Contributor Author

KATT commented May 27, 2021

workaround in my monorepo: trpc/trpc@ff184a9

@glitch452
Copy link

I also experienced the same issue. Reverting to TS v4.2.4 solved it for now.
Here are the errors I see. Hope it helps!

error TS2321: Excessive stack depth comparing types 'ZodOptional<?>' and 'ZodOptional<?>'.
error TS2321: Excessive stack depth comparing types 'ZodError<?>' and 'ZodError<?>'.

z.union([z.string(), z.number(), z.boolean(), z.null()]);
error TS2321: Excessive stack depth comparing types 'ZodFormattedError<?>' and 'ZodFormattedError<?>'.

z.union([z.string(), z.number(), z.boolean(), z.null()]);
error TS2321: Excessive stack depth comparing types 'ZodIntersection<?, U>' and 'ZodIntersection<?, U>'.

z.union([z.string(), z.number(), z.boolean(), z.null()]);

error TS2321: Excessive stack depth comparing types 'ZodIntersection<T, ?>' and 'ZodIntersection<T, ?>'.
z.union([z.string(), z.number(), z.boolean(), z.null()]);

src/services/schemas.ts:10:30 - error TS2321: Excessive stack depth comparing types 'ZodNonEmptyArray<?>' and 'ZodNonEmptyArray<?>'.
z.union([z.string(), z.number(), z.boolean(), z.null()]);

error TS2321: Excessive stack depth comparing types 'ZodRecord<?>' and 'ZodRecord<?>'.
z.record(z.string().or(z.number())).optional(),

Ps. Love the library, keep up the awesome work!

@adam-thomas-privitar
Copy link

adam-thomas-privitar commented Jun 2, 2021

Hi guys. This has been raised on microsoft/TypeScript#44299. There seems to be a more severe variation of this where node runs out of memory during type checking when zod is in use.

@colinhacks
Copy link
Owner

Fixed in zod@3.2 - upgrade and it will work with TypeScript 4.3.2.

This was a doozy. TS 4.3 added limitations on the self-referential recursive conditional types inside class methods, something that Zod relies on heavily.

I managed to get everything working again with minimal disruption to the Zod's internals, but there's one minor change that was unavoidable. Certain methods (.or and .transform) now return a new instance that wrap the current instance. Previously Zod would try to avoid additional/unnecessary nesting. For example:

z.union([z.string(), z.number()]).or(z.boolean());
// previously
// => ZodUnion<[ZodString, ZodNumber, ZodBoolean]>

// now
// => ZodUnion<[ZodUnion<[ZodString, ZodNumber]>, ZodBoolean]>

With TypeScript 4.3.2 it was no longer possible to reflect the old type signature. In practice I think this change is minor and could almost be considered an internal change so I'm not bumping the major version. This behavior is actually more consistent and avoids certain pitfalls when implementing generic functions that operate on ZodSchemas, so it's not all bad.

eventualbuddha added a commit to votingworks/vxsuite that referenced this issue Jun 13, 2021
Mainly for this: colinhacks/zod#473. We're not using TS 4.2.3 yet, but latest VS Code uses it by default and causes issues.
eventualbuddha added a commit to votingworks/vxsuite that referenced this issue Jun 13, 2021
Mainly for this: colinhacks/zod#473. We're not using TS 4.2.3 yet, but latest VS Code uses it by default and causes issues.
eventualbuddha added a commit to votingworks/vxsuite that referenced this issue Jun 13, 2021
Mainly for this: colinhacks/zod#473. We're not using TS 4.2.3 yet, but latest VS Code uses it by default and causes issues.
toresbe added a commit to nais/dp that referenced this issue Jun 14, 2021
The version-pin workaround for colinhacks/zod#473 is no longer
necessary. Also upgraded yarn packages in general.
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

6 participants