Convert a zod object schema to a record type

  Kiến thức lập trình
const definition = z.object({
    a: z.string(),
    b: z.number(),
})

I want to have a type like this

{
    a?: string,
    b?: number,
}

I don’t want to convert the object schema to make every key optional because it’s used as a definition map to match a sub schema to a key. So what I really want is a Record type

z.record(definition.keyof(), definition.shape[myKey])

Is it possible to achieve this?

LEAVE A COMMENT