[Typescript] Zod.input()

The Input is Different than the Output

We've reached the point with Zod where our input is different than our output.

In other words, you can generate types based on input as well as generating types based on the output.

For example, let's create FormInput and FormOutput types:

 
type FormInput = z.infer<typeof Form>
type FormOutput = z.infer<typeof Form>

 

Introducing z.input

As written above, the input is not quite correct because when we input into validateFormInput, we don't need to pass in any keywords.

Instead, we can update the the FormInput to use z.input instead of z.infer.

This gives us a way to slightly tweak the types we generate if there is a difference between the input and output of our validator.

posted @ 2024-12-21 19:19  Zhentiw  阅读(8)  评论(0)    收藏  举报