how do i schema.parse with returned type from schema? #1556
-
hi i tried to get return type of zod.parse using the code below, but visual code does not give any autocomplete on aaa. thank you const inputSchema = z.object({
page: z.string().default('1').transform((v) => parseInt(v)),
limit: z.string().default('10').transform((v) => parseInt(v)),
})
export const parseTest = async (schema: ZodType, data: any): Promise<z.infer<typeof schema>> => {
return schema.parse(schema, data)
}
const aaa = await parseTest(inputSchema,{page: '1', limit: '10'}) |
Beta Was this translation helpful? Give feedback.
Answered by
JacobWeisenburger
Dec 5, 2022
Replies: 1 comment
-
I think you need to use a generic. Let me know if you have any questions. const inputSchema = z.object( {
page: z.string().default( '1' ).transform( ( v ) => parseInt( v ) ),
limit: z.string().default( '10' ).transform( ( v ) => parseInt( v ) ),
} )
export const parseTest = async <Schema extends z.ZodType> (
schema: Schema, data: any
): Promise<z.infer<Schema>> => {
return schema.parse( schema, data )
}
const aaa = await parseTest( inputSchema, { page: '1', limit: '10' } )
type AAA = typeof aaa
// type AAA = { page: number; limit: number; }
console.log( aaa ) // { page: 1, limit: 10 } |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
JacobWeisenburger
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you need to use a generic.
<Schema extends z.ZodType>
Let me know if you have any questions.