Last active
October 25, 2025 21:40
-
-
Save mmikhan/e61e13611f88686c2d484b5f2fefbe75 to your computer and use it in GitHub Desktop.
Zod object from TypeScript interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @credit https://github.com/colinhacks/zod/issues/2807#issuecomment-2962935341 | |
| import z from 'zod' | |
| export interface Product { | |
| title: string; | |
| description: string; | |
| } | |
| type ProductObjectShape<T extends Product> = { | |
| [K in keyof T]: z.ZodType<T[K]> | |
| } | |
| const formSchema = z.object<ProductObjectShape<Product>>({ | |
| title: z.string(), | |
| description: z.string(), | |
| }) | |
| // Alternative | |
| const formSchema: z.ZodType<Partial<Product>> = z.object({ | |
| title: z.string(), | |
| description: z.string(), | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment