Is it possible to pass a zod schema as a prop?

I have this
interface Props {
    schema: ZodSchema;
    children: React.ReactNode;
    defaultValues?: unknown;
}

what is the proper way to type the schema prop on the receiving end?
Solution
owo7 if you want to accept any zod schema then you could do something like
import type { ZodSchema } from "zod"

type ComponentProps<TSchema extends ZodSchema> = {
    schema: TSchema,
    children: React.ReactNode
}
function Component<TSchema extends ZodSchema>({schema, children}:ComponentProps<TSchema>){
    return <>{children}</>
}

But if want to accept a specific schema
const schemaIn = z.object({
    foo: z.literal('bar')
})

type ComponentProps = {
    schema: typeof schemaIn,
    children: React.ReactNode
}
function Component({schema, children}:ComponentProps){
    return <>{children}</>
}
Was this page helpful?