-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve type inference from Zod schemas #114
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: why doesn't z.infer
just do this already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the weirdness only happens on zod types with .passthrough()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: