Problem with coerce in zod 4.4.2 #5942
-
|
Hi everyone This is my time using zod and i run into a problem that i dont know ho to handle. I am using zod + react-hook-form in my react app price: z
.coerce
.number('El precio debe de ser un número')
.positive('El precio debe ser mayor a cero')I am using the inferred type for the resolver in useForm: export type ServiceFormData = z.infer<typeof createServiceSchema>
const {
register,
handleSubmit,
formState: { errors, isSubmitting }
} = useForm<ServiceFormData>({
resolver: zodResolver(createServiceSchema)
})The problem is that coercion apparently makes the price field infer as unknow, but my solve for useForm expects it to be number. I already tried it at runtime and the program works, but I don't know how to solve that problem in the compiler This is the error Type 'Resolver<{ name: string; price: unknown; duration_hours: number; duration_minutes: number; description: string; }, any, { name: string; price: number; duration_hours: number; duration_minutes: number; description: string; }>' is not assignable to type 'Resolver<{ name: string; price: number; duration_hours: number; duration_minutes: number; description: string; }, any, { name: string; price: number; duration_hours: number; duration_minutes: number; description: string; }>'. Types of parameters 'options' and 'options' are incompatible. Type 'ResolverOptions<{ name: string; price: number; duration_hours: number; duration_minutes: number; description: string; }>' is not assignable to type 'ResolverOptions<{ name: string; price: unknown; duration_hours: number; duration_minutes: number; description: string; }>'. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Using coerce will lead to an input type of |
Beta Was this translation helpful? Give feedback.
Using coerce will lead to an input type of
unknownand an output type ofnumber. The schema will have different input and output types, which you can get withz.input<>andz.output<>. You can provide three generics touseForm(TFieldValues,TContext,TTransformedValues) or allow them to be inferred from thezodResolver.See this TypeScript playground.