-
-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue: resolver parameter of useForm hook is wrong typed #735
Comments
@bluebill1049 Could you please link the duplicate issue? |
Hi @supersnager, I have transferred your issue to the Resolvers repo. As a workaround, here’s what I can suggest: https://codesandbox.io/p/sandbox/react-hook-form-and-yup-forked-j35vtl You need to specify the type for both interface FormInput {
name: string | null;
}
interface FormOutput {
name: string;
}
const schema: yup.ObjectSchema<FormOutput> = yup.object({
name: yup.string().required("Name is required"),
});
const { handleSubmit } = useForm<FormInput, any, yup.InferType<typeof schema>>({
resolver: yupResolver<FormInput>(schema),
defaultValues: {
name: null,
},
}); Let me know what you think. |
@jorisre Thank you for checking that. Your workaroud is the same as my mentioned in the comment in my codesandbox (https://codesandbox.io/p/sandbox/react-hook-form-and-yup-ytml5y), so I already know it, and I use it everywhere in my projects :). If it's not possible to fix this without doing breaking change, maybe it's time to release a new major version? |
bump |
A pull request is currently in progress. #753 |
Version Number
7.51.1
Codesandbox/Expo snack
https://codesandbox.io/p/sandbox/react-hook-form-and-yup-ytml5y
Steps to reproduce
Explanation
First of all thank you guys for this awesome library!
I 'm migrating my project from Formik to RHF and it's a really good experience, but I have found a bug with typings.
What I want to achieve is that my FormInput type have some properties that can have a null value.
In empty form, I want to set for example select field (ReactSelect) to empty value.
The field is nullable, but user has to fill it to submit the form.
Therefore I want my onsubmit type (FormOutput) should be validated version of FormInput.
Hoverver when the example is correctly typed, the Typescript error appears.
This is because the expected type of the resolver property is
Resolver<TFieldValues, TContext>
(Resolver)but
yupResolver
function returnsResolver<Yup.InferType<typeof schema>
(Resolver) and these types doesn't matchPlease also see the comments in the linked sandbox.
I'm not sure, but It seems to me that the fix should be something similar to this fix react-hook-form/react-hook-form@662f2c6
Expected behaviour
No errors
What browsers are you seeing the problem on?
Chrome
Relevant log output
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: