Repository navigation
Improve type inference from Zod schemas - #114
Conversation
jerome3o-anthropic
left a comment
There was a problem hiding this comment.
awesome - tested in my ide and the hover tips are soo much better.
I don't really understand why there isn't a nested z.infer already. But this works!
| ? { [K in keyof T]: Flatten<T[K]> } | ||
| : T; | ||
|
|
||
| type Infer<Schema extends ZodTypeAny> = Flatten<z.infer<Schema>>; |
There was a problem hiding this comment.
q: why doesn't z.infer just do this already?
There was a problem hiding this comment.
Looks like the weirdness only happens on zod types with .passthrough().
There was a problem hiding this comment.
Honestly, no idea. The only relevant thing I could find is https://github.com/colinhacks/zod/blob/f7ad26147ba291cb3fb257545972a8e00e767470/src/helpers/partialUtil.ts, which doesn't apply to general z.infer usage—only benefitted a feature that is now deprecated in Zod.
I'm surprised not to see even an issue in the backlog that looks related here. 🤷
Adds some type utilities that attempt to flatten and simplify inferred Zod types, for better readability in hoverover, typechecking, and IDE features.
Since TypeScript is structurally typed, not nominally typed, this shouldn't be a breaking change (as long as the resultant object types are indeed the same structurally!).
For example, a couple of types before:
And after: