Closed kotarella1110 closed 12 months ago
Size Change: 0 B
Total Size: 25.4 kB
This PR does not aim to resolve https://github.com/react-hook-form/resolvers/issues/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),
:tada: This PR is included in version 3.3.1 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
Thanks a ton @kotarella1110
Overview
This PR addresses the issue outlined in https://github.com/react-hook-form/resolvers/issues/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