partialSafeParse data inference not working #16
Replies: 3 comments 1 reply
|
If you are only wanting to access the const schema = z.object( { id: z.number() } )
const parsed = schema.safeParse( { id: 1 } )
if ( parsed.success ) {
type Data = typeof parsed.data
// type Data = { id: number }
}
const schema = z.object( { id: z.number(), name: z.string() } )
const parsed = zu.partialSafeParse( schema, { id: 1, name: false } )
console.log( parsed.validData ) // { id: 1 }
type ValidData = typeof parsed.validData
// type ValidData = {
// name?: string | undefined
// id?: number | undefined
// }
console.log( parsed.invalidData ) // { name: false }
type InvalidData = typeof parsed.invalidData
// type InvalidData = {
// name?: string | undefined
// id?: number | undefined
// } |
|
Hi, thanks for the quick answer. I use the best of both worlds, but for the repro example I just tried to show the problem in the simplest way possible In my code what I do is a recovery system following these steps:
My expectation with partialSafeParse is to have the same API of safeParse but with vitamins for partial data. |
|
zod_utilz/src/partialSafeParse.ts Lines 31 to 37 in 622cf88 |

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
this code (similar to just plain safeParse) should infer the type of data if the validation is successful
Instead the type inferred is unknown. The same happens to the ZodErrors type
All reactions