Skip to content

Instantly share code, notes, and snippets.

@mmikhan
Last active October 25, 2025 21:40
Show Gist options
  • Save mmikhan/e61e13611f88686c2d484b5f2fefbe75 to your computer and use it in GitHub Desktop.
Save mmikhan/e61e13611f88686c2d484b5f2fefbe75 to your computer and use it in GitHub Desktop.
Zod object from TypeScript interface
// @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