saiichihashimoto / sanity-typed-schema-builder

Build Sanity schemas declaratively and get typescript types of schema values for free!
https://saiichihashimoto.github.io/sanity-typed-schema-builder
MIT License
67 stars 5 forks source link

Typescript errors with `createType` zod versions != 3.17.10 #204

Open donalffons opened 1 year ago

donalffons commented 1 year ago

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 commented 1 year ago

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?!

saiichihashimoto commented 1 year ago

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.

DanielMontilla commented 1 year ago

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

saiichihashimoto commented 1 year ago

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?

DanielMontilla commented 1 year ago

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

donalffons commented 1 year ago

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.

danteissaias commented 1 year ago

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?

donalffons commented 1 year ago

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

saiichihashimoto commented 1 year ago

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.

danteissaias commented 1 year ago

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?

mckelveygreg commented 1 year ago

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

saiichihashimoto commented 1 year ago

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

danteissaias commented 1 year ago

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