-
Notifications
You must be signed in to change notification settings - Fork 4
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
Is there way to add typing to .meta()? #1
Comments
Hello, thank you for your attention to this issue! I tried both approaches and the original one works better for my purposes. Let me explain. (A) Defining global type for all
import z from 'zod';
declare module 'zod' {
interface TMeta {
[k: string]: unknown;
}
interface ZodTypeDef {
meta?: TMeta;
}
interface ZodType {
getMeta(): TMeta;
meta(meta: TMeta): this;
}
}
export declare function register(zod: typeof z): void; And overriding TMeta interface in project, for example in import z from 'zod';
declare module 'zod' {
interface TMeta {
name: string;
age?: number;
}
} Finally, we have this: (B) Approach from #2 automagically infers type from Please consider this pull request #3 with updated documentation and examples. |
Hello, nice approach, few weeks ago I implemented someting similiar but there is no reason to duplicate similiar packages. And I really confused why zod author won't support this feature natively, however I understand his arguments.
Now I think about the way to make this lib support typing of data containing in meta. I tried to override default type
Record<string, unknown>
it in standaloned.ts
but this unfortunately does not work (this only works when I edit embeddednode_modules/zod-metadata/dist/index.d.ts
):What do you thing about making at least optionally generic
getMeta
andmeta
methods, like this?This is ugly, of course, but I really don't see other options to override meta typing. Maybe you have some ideas?
UPD: I solved overriding issue: first line
import z from 'zod';
is neccessary to make it work, so we have a typed meta now! )<root>/typings/zod-metadata.d.ts:
And to make it work we need to slightly change
node_modules/zod-metadata/dist/index.d.ts:
This works because interfaces in TS can be extended, so we exactly extending
TMeta
.The text was updated successfully, but these errors were encountered: