-
-
Notifications
You must be signed in to change notification settings - Fork 167
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
fix(yup): resolve ts error when including optional key in yup schema #625
Conversation
Size Change: 0 B Total Size: 25.4 kB ℹ️ View Unchanged
|
This PR does not aim to resolve #589. However, regarding this issue, as mentioned in https://github.com/orgs/react-hook-form/discussions/10618#discussioncomment-6826058, I believe using the FormValue's generics should be passed to yupResolver to address the inconsistency issue. resolver: yupResolver<FormValues>(schema), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks @kotarella1110
🎉 This PR is included in version 3.3.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Thanks a ton @kotarella1110 |
Overview
This PR addresses the issue outlined in #575. The problem stems from inaccurate inference of generics (
TFieldValue
) inyupResolver
.For instance, when passing a schema with optional keys to
yupResolver
:The inferred type for
yupResolver
looks like this:As you can see, the generics (
TFieldValue
) foryupResolver
should have been inferred as optional keys, but they were inferred as required keys instead. This issue arises from the fact that theObjectSchema
in Yup extends the underlying schema's first argument usingMakeKeysOptional
. This utility type makes keys optional when the value of an object property isundefined
. However, because this utility type is used, the expected inference isn't achieved, resulting in keys being mistakenly inferred as required.https://github.com/jquense/yup/blob/1ee9b21c994b4293f3ab338119dc17ab2f4e284c/src/object.ts#L99
The incorrect inference of generics (
TFieldValue
) was causing problems when passing it as the form type to the Resolver. To resolve this, we fixed the return type by passing the correctly inferred schema type obtained throughYup.InferType
as the argument to the Resolver.Resolved CSB
You can view the resolved example in the following CSB link: https://codesandbox.io/s/rhf-625-54jvv4