colinhacks / zod

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

Workarounds for TS 7056? #3719

Open dielsalder opened 3 months ago

dielsalder commented 3 months ago

Hi guys. I'm pretty new to Zod. I have a project with some large schemas and I am frequently running into this error:

TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.

It seems that it has to do mainly with typescript hitting a hard maximum for schemas of a certain size, as I can trigger it just by adding or removing one property on a schema. Splitting the big schema into multiple variables or files does not help.

Would like to know if there are any good workarounds to this. There has been a past issue but none of the solutions in that thread have helped for me. The only thing I haven't tried is to monkey-patch tsc, but I would like to avoid that.

Thanks!

colinhacks commented 3 months ago

Is there a reason you need declaration: true in your tsconfig? Typically this isn't necessary for application code. https://stackoverflow.com/questions/68463963/typescript-the-inferred-type-of-this-node-exceeds-the-maximum-length-the-compi

If you need declaration files for some reason, you may have to tweak the generation code to include explicit type declarations, as the error message says.

If that's not workable, I'm afraid you'll have to wait for Zod 4 which will dramatically simplify a lot of internal type representations. Sorry for not having better news, that error is always a doozy.

everhardt commented 3 months ago

Is there a reason you need declaration: true in your tsconfig?

Monorepos are: they require composite: true, which in turn requires declaration: true.

thelinuxlich commented 2 months ago

I work in a monorepo and I don't need composite neither declaration.

catz commented 2 months ago

In our project, it happens on typescript 5.6.2 but 5.5.4 works fine.

everhardt commented 1 month ago

@thelinuxlich When I set "declaration": false I get the following error:

TS6304: Composite projects may not disable declaration emit.

thelinuxlich commented 1 month ago

Like I said, I don't use composite on my monorepo.

everhardt commented 1 month ago

Could you please share a bit more about your setup? You cannot disable declaration if you set composite: true. And that in turn is required if you want to make use of references, which I would say is the essence of a monorepo.

thelinuxlich commented 1 month ago

Please take a look at this great article by Colin: https://colinhacks.com/essays/live-types-typescript-monorepo

I go with the third approach, using paths