colinhacks / zod

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

Workarounds for TS 7056? #3719

Open dielsalder opened 1 month ago

dielsalder commented 1 month 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 weeks 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 2 weeks 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 1 week ago

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

catz commented 2 days ago

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