Replies: 1 comment
-
I can't speak to if this was by design or not, but this code might help you. type ShapeFromType<Type extends Record<string, any> | undefined> =
Type extends undefined ? never : {
[ key in keyof Type ]-?:
unknown extends Type[ key ] ? z.ZodType<Type[ key ]> | z.ZodOptional<z.ZodType<Type[ key ]>> :
undefined & null extends Type[ key ] ?
z.ZodNullable<z.ZodOptional<z.ZodType<Type[ key ]>>> | z.ZodOptional<z.ZodNullable<z.ZodType<Type[ key ]>>> :
undefined extends Type[ key ] ? z.ZodOptional<z.ZodType<Type[ key ]>> :
null extends Type[ key ] ? z.ZodNullable<z.ZodType<Type[ key ]>> :
z.ZodType<Type[ key ]>
}
type Temp = {
a?: string | null
}
const guard = z.object<ShapeFromType<Temp>>( {
a: z.string().nullable().optional()
} ) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was trying to infer a ZodType from a well-defined typescript type: e.g.
The following ZodType definition is considered as a valid ZodSchema type.
What I am expected is it should throw an error like
ZodString
type is not assignable toZodOptional<ZodNullable<ZodString>>
only
Is strictly allowed.
Wondering whether this is by design or not?
Thanks~
Beta Was this translation helpful? Give feedback.
All reactions