useForm with zod and typescript #589
-
Hello everybody, I'm uncertain on how to use the const { isIdle, mutate: authenticate } = useMutation<undefined | string, DefaultError, LoginFormDto>({
mutationFn: async dto => {
return await authenticationRepository.authenticate(dto);
},
});
const form = useForm({
validatorAdapter: zodValidator,
onSubmit: ({ value }) => {
authenticate(value); // Typescript will complain because value is of type unknown
},
}); While this is totally expected, since I'm not providing any schema that can be used to infer the type of Also if I provide the schema under the validators, the type will not be desumed const { isIdle, mutate: authenticate } = useMutation<undefined | string, DefaultError, LoginFormDto>({
mutationFn: async dto => {
return await authenticationRepository.authenticate(dto);
},
});
const form = useForm({
validatorAdapter: zodValidator,
validators: {
onSubmit: loginFormSchema,
},
onSubmit: ({ value }) => {
authenticate(value); // Typescript will complain because value is of type unknown
},
}); Also I've seen that if I provide the types to the const { isIdle, mutate: authenticate } = useMutation<undefined | string, DefaultError, LoginFormDto>({
mutationFn: async dto => {
return await authenticationRepository.authenticate(dto);
},
});
const form = useForm<LoginFormDto, typeof zodValidator>({
validatorAdapter: zodValidator,
onSubmit: ({ value }) => {
authenticate(value);
},
});
const form = useForm({
validatorAdapter: zodValidator,
validators: loginFormSchema,
onSubmit: ({ value }) => {
authenticate(value); // TS is "smart enough" to know value's type
},
}); What do you think? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
This appears to be a bug with our types, not a limitation with our API. const { isIdle, mutate: authenticate } = useMutation<undefined | string, DefaultError, LoginFormDto>({
mutationFn: async dto => {
return await authenticationRepository.authenticate(dto);
},
});
const form = useForm({
validatorAdapter: zodValidator,
validators: {
onChange: loginFormSchema,
},
onSubmit: ({ value }) => {
authenticate(value);
},
}); This code should work as-intended with Could you open a GH issue reporting this bug so we can investigate?
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
For anyone still dealing with this. This seems to work for me. Not sure why I have to call the zodValidator though.
|
Beta Was this translation helpful? Give feedback.
-
Nevermind,for some reason the Typescript server bugged out and I had to restart VS Code lol.
export const FormSchema = z.object({
email: z.string(),
companies: z.array(CompanySchema).optional().default([]),
});
export type UserForm = z.infer<typeof FormSchema>;
const form = useForm<UserForm, ZodValidator>({
defaultValues: {
email: "",
companies: [],
},
validatorAdapter: zodValidator(),
validators: {
onChange: FormSchema,
}
});
|
Beta Was this translation helpful? Give feedback.
This appears to be a bug with our types, not a limitation with our API.
This code should work as-intended with
value
defined properly andonChange
executing for the whole app.Could you open a GH issue reporting this bug so we can investigate?