colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.28k stars 1.15k forks source link

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

Open ADSNB opened 7 months ago

ADSNB commented 7 months ago

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.

samchungy commented 6 months ago

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.

colinhacks commented 6 months ago

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.

tonikny commented 6 months ago

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.

medflow-io-luke commented 5 months ago

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.

jeromeetienne commented 3 months ago

something similar happened to me too. sharing my experience here, i spend a day trying to understand the matter. maybe it will help somebody else

having a single Zod installed in a single package.json made it come back to normal time.