#Unable to use Zod parse () without assertion

12 messages · Page 1 of 1 (latest)

languid tide
#

I want to create an utility function which takes ANY zod schema and parses it well

#

However that's what I can only do - add as unknown as T

#

Is this the only way?

#

Omg

#

Wrong example it was

#

Ignore me

#

Let me prepare a correct test case

#

Ok, I think I cannot come up with anything different than this:

misty atlasBOT
#
onkeltem#0

Preview:```ts
import type {ZodObject, ZodRawShape} from "zod"

function validate<T extends ZodRawShape>(
data: T,
schema: ZodObject<T>
): T {
const res = schema.parse(data)
return res as unknown as T
}```

languid tide
#

So... if I want to type future Zodd Object Schema, I should pass it the data type, right?

#

Ok, I believe I should have read the docs first

#

Correct code (presumably):