You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for creating this great library. Save me a ton of work in my current project.
I have many Zod schemas having properties with default values. z.infer gives z.output inference by default. However, when sending data to the constructor, I find it convenient to use z.input which marks the properties with default value as optional - ref
To illustrate
constUserProfileSchema=z.object({name: z.string(),age: z.number(),role: z.string().default("Admin"),});classUserProfileextendsZ.class(UserProfileSchema.shape){// constructor(input: z.input<typeof UserProfileSchema>) {// super(input as z.output<typeof UserProfileSchema>);// }getMessage(){return`Hello ${this.name}, you are ${String(this.age)} years old and your role is ${this.role}`;}}constprofile=newUserProfile({name: "John",age: 30});// gives TS error unless constructor code is uncommentedprofile.getMessage();
Is there a way to send z.input based schema inference for the constructor parameters without resorting to overloading the constructor? While overloading a constructor is not a big deal, it just becomes repeatative for all my Zod classes.
The text was updated successfully, but these errors were encountered:
Thanks for creating this great library. Save me a ton of work in my current project.
I have many
Zod schemas
having properties with default values.z.infer
givesz.output
inference by default. However, when sending data to the constructor, I find it convenient to usez.input
which marks the properties with default value as optional - refTo illustrate
Here's the playground.
Is there a way to send
z.input
based schema inference for the constructor parameters without resorting to overloading the constructor? While overloading a constructor is not a big deal, it just becomes repeatative for all my Zod classes.The text was updated successfully, but these errors were encountered: