zod icon indicating copy to clipboard operation
zod copied to clipboard

Using "zod.infer" in a specific object will cause the vs code typescript intellisense stuck forever in "Loading..."

Open ADSNB opened this issue 5 months ago • 5 comments

Hello gyes!

I'm new using zod as validator for objects. I have found a very frustrating bug! When I use zod infer to create a new type of object based on a zod object my visual studio code intellisense stops working. When this happen I got eternal vs code "Loading..." status message and in the bottom corner of the screen says "Loading Intellisense Status..." and this stays forever in this status when I try to use zod infer method.

Open the file "src\app\comp1\constructionCompany\component\test.tsx".

If we comment z.infer line the intellisense will work: Untitled

If we uncomment z.infer line the intellisense will stop working in entire project: Untitled2

I created a repo where we can reproduce the error with the vs code intellisense. Basically to initialize the repo you should execute "npm install" and "npx prisma generate" to generate the schemas and objects from prisma database.

Repo: https://github.com/ADSNB/intellisense-bug-zod-infer

I'm very frustrated because I have spent some days to identify what exactly was breaking the intellisence in this project that I'm working and I was unable to find a fix yet on my own. I'm using zod with trpc prisma generator to create classes with automatic validation and this worked for simple tables without relation between them. But in this specific scenario we have a table called "ConstructionCompany" and another table "ConstructionCompanyProject" and this tables have an relation one to many. I guess when I use zod infer in this type of object for some reason this doesn't work. I tried to use developer console to check the console logs and analyse TS server logs but I didn't found anything that could indicates or solve this issue.

Any help is really appreciated. If you have any questions let me know.

ADSNB avatar Feb 12 '24 22:02 ADSNB

It's likely that the zod instance your schema was written in may have been created using a different version of zod. The tsc compiler doesn't do too well in that scenario and is likely failing.

samchungy avatar Mar 14 '24 23:03 samchungy

I understand your frustration here and I've encountered some similar infinite loop problems before.

That's a lot of generated code and there are a lot of recursive types there. Seems that referencing this type in your hook is forcing TypeScript to eagerly resolve an incredibly complex type and it's struggling. Unfortunately I don't have an easy fix right now but it's useful to have a reproduction.

colinhacks avatar Mar 15 '24 00:03 colinhacks

I have encountered the same problem. Not only does the intellisense fail, but it also eslint and more, so it doesn't show errors in the code. Running lint from terminal takes a looong time to finish.

tonikny avatar Apr 04 '24 16:04 tonikny

I have also encountered this error.

I downgraded my zod version and for some reason it started working again but I am not actively calling the z.infer anywhere to my knowledge.

medflow-io-luke avatar May 01 '24 21:05 medflow-io-luke