-
-
Notifications
You must be signed in to change notification settings - Fork 157
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 resolvers typings with react-hook-form@7.15.0
#233
Fix resolvers typings with react-hook-form@7.15.0
#233
Conversation
thanks for the contribution 💪 |
react-hook-form@7.15.0
@bluebill1049 Any idea when this'll get merged? |
let's wait for @jorisre he is the master mind behind resolver. |
@bluebill1049 Any idea when @jorisre will be available to review this? Upgrading to the latest version of react-hook-form is impossible because of this issue. |
Hey @julienfouilhe I will review it now, and I have also pinged Joris too. |
export type Resolver = < | ||
T extends { [_: string]: any }, | ||
TFieldValues extends FieldValues, | ||
TContext, |
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.
👍
@@ -64,22 +64,22 @@ export const zodResolver: Resolver = | |||
options.shouldUseNativeValidation && validateFieldsNatively({}, options); | |||
|
|||
return { | |||
errors: {}, | |||
errors: {} as FieldErrors<any>, |
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.
wondering why have to start casting now.
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.
I don't know exactly but seems like an empty object is not assignable to the DeepMap with the new version of FieldErrors from react-hook-form?
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.
Sorry for the delay. I'll look at it tonight |
Merci |
@jorisre What would be the expected signature? I feel like values should always be typed unknown, and we should infer the TFieldValues from the schema, like this: import { ResolverResult, ResolverOptions, FieldValues } from 'react-hook-form';
import { z } from 'zod';
export type Resolver = <TFieldValues extends FieldValues, TContext>(
schema: z.Schema<TFieldValues, any>,
schemaOptions?: Partial<z.ParseParamsNoData>,
factoryOptions?: { mode?: 'async' | 'sync' },
) => (
values: unknown,
context: TContext | undefined,
options: ResolverOptions<TFieldValues>,
) => Promise<ResolverResult<TFieldValues>>; This would actually allow not to specify the FieldValues to useForm({
resolver: zodResolver(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.
Thank you for the contribution @julienfouilhe !
LGTM! I'll make some minor changes in another PR.
Sorry again for the delay.
@jorisre I fixed the tests sorry about that. Thought I had run them. I had to add unpacking NestedValue support to io-ts |
🎉 This PR is included in version 2.8.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
thank you @julienfouilhe & @jorisre ❤️ |
I upgraded to the latest
react-hook-form
and@hookform/resolvers
versions and all of a sudden all my forms that were using theyupResolver
were throwing a typescript error while those using the io-ts resolver (I'm progressively migrating to io-ts) didn't.I checked what could be the source and figured the typings were not infered properly when wrapping the generic function.
Example of something that does not work:
Fixes #234