sanity-typed-schema-builder icon indicating copy to clipboard operation
sanity-typed-schema-builder copied to clipboard

Typescript errors with `createType` zod versions != 3.17.10

Open donalffons opened this issue 1 year ago • 13 comments

Thank you for this great package! I've been enjoying using it a lot!

Here is my issue: When I copy the example code for createType into my project / code editor (VS code), it takes a very long time to do perform type checking and then eventually fails with multiple instances of the error "Type instantiation is excessively deep and possibly infinite". It doesn't happen with older zod versions.

image

donalffons avatar Mar 10 '23 09:03 donalffons

oh well, the same thing happens if I use an older zod version - I just tested 3.16.1. Maybe that means that my zod version has to match the one used by this package?!

donalffons avatar Mar 10 '23 09:03 donalffons

I'm not able to repro this. Are you able to create a minimal repro that illustrates what you're talking about?

I also haven't tested this with various versions of zod. There might be a better way to include/use zod in this library, but I haven't tried anything.

saiichihashimoto avatar Mar 13 '23 17:03 saiichihashimoto

matching zod version (3.17.10) fixed it for me. Heres a repo to reproduce it.

DanielMontilla avatar Apr 09 '23 17:04 DanielMontilla

This is hard to solve. zod is bundled with this package on purpose, so it will always be a specific version of zod. I could make it a peerDependency, but I'd like to avoid that. Are you using zod for other reasons or are you installing it specifically for this library?

saiichihashimoto avatar Apr 18 '23 22:04 saiichihashimoto

I'm also using zod for other stuff but reverting to the old version didn't seem to break anything

DanielMontilla avatar Apr 21 '23 18:04 DanielMontilla

Apologies for the late reply and thanks a lot for looking into this. This issue isn't critical for me any more - I decided to work around this issue by avoiding to use custom types. Unfortunately, I can't think of any better solution, at the moment.

donalffons avatar Apr 27 '23 07:04 donalffons

This is hard to solve. zod is bundled with this package on purpose, so it will always be a specific version of zod. I could make it a peerDependency, but I'd like to avoid that. Are you using zod for other reasons or are you installing it specifically for this library?

Is there a reason to avoid making it a peer dependency?

danteissaias avatar Apr 28 '23 09:04 danteissaias

Is there a reason to avoid making it a peer dependency?

I would assume that setting the exact version as a peerDependency would force the package manager to always resolve to that specific version of zod. If other packages require slightly different versions, the package manager might fail to resolve. This would prevent users from using this library in their projects, even though they might never run into the problem described above (since this problem only occurs once you start using createType). That's my understanding, but certainly @saiichihashimoto knows best :slightly_smiling_face:.

donalffons avatar Apr 28 '23 09:04 donalffons

I doubt it'll end up mattering too much if I make zod a peerDependency, although it's asking someone to install zod when they may not need to, which is always a bit annoying. My bigger qualm is that I'm not sure why it's causing a collision at all. Shouldn't this package just use it's own version of zod and not collide? Not sure what's going on.

saiichihashimoto avatar Apr 28 '23 21:04 saiichihashimoto

I doubt it'll end up mattering too much if I make zod a peerDependency, although it's asking someone to install zod when they may not need to, which is always a bit annoying. My bigger qualm is that I'm not sure why it's causing a collision at all. Shouldn't this package just use it's own version of zod and not collide? Not sure what's going on.

I’m using pnpm, might be an issue with how pnpm constructs node_modules?

danteissaias avatar Apr 28 '23 22:04 danteissaias

I'm in a monorepo with other packages using zod, so if I use an exported schema in another package to combine it with that package's zod, that dependent package must also have the same version or zod is unhappy...

mckelveygreg avatar Apr 28 '23 22:04 mckelveygreg

@danteissaias, are you able to see if using npm directly has the same issue in your project? I'm curious if there's an pnpm specific problem here.

saiichihashimoto avatar Apr 29 '23 21:04 saiichihashimoto

This got fixed for me by upgrading to the latest versions of both zod and sanity-typed-schema builder. 👍

danteissaias avatar May 05 '23 12:05 danteissaias