-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
v4: z.interface() not suport jsDoc #4108
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 commun 8000 ity.
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
Comments
How about adding a method .deprecated() and adding the schema to the metadata? https://json-schema.org/draft/2020-12/json-schema-validation#section-9.3 |
That might be good in some cases, but not for mine — I should've included my use case in the first comment. In my use case, I rely on z.infer to define my DTOs, so I can avoid duplicating type definitions and keep validation logic decoupled from TypeScript interfaces. However, when JSDoc annotations (not just @deprecated, but any comments) are lost in the inferred type, I’m forced to manually define a separate TypeScript interface just to preserve documentation metadata — which defeats part of the purpose of using z.infer. Additionally, if z.object could support getters for defining cyclical properties, that would be great. |
I would wager it's the optional key behaviour that strips the JSDoc |
Yes, you're right. But I've found that any type inference or transformation causes JSDoc to be lost, including:
At this point, I believe this isn't a bug in Zod per se — it's a TypeScript limitation. Here’s a minimal example: const original = {
/** user id */
id: true,
} as const;
type Original = typeof original;
const transformed = {
get id() {
return original.id;
},
};
transformed.id; // JSDoc lost
const typeInfer = () => original;
typeInfer().id; // JSDoc lost It might be helpful if Zod exposes something like an originalShape property — |
|
I noticed an issue where JSDoc comments, such as @deprecated, are not preserved after using z.infer on z.interface(), while they are correctly retained when using z.object().
📷 Screenshot
In the screenshot below, you can see that the @deprecated annotation appears in the inferred type from z.object(), but not from z.interface():
🧪 Reproduction
🧠 Expected Behavior
I expected z.infer to retain JSDoc metadata like @deprecated, just like z.object() does.
The text was updated successfully, but these errors were encountered: