TypeScript: What type for register (instead of any) when calling it as a prop? #4426
-
I read the TypeScript documentation, but I don't understand which type to use for For example:
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 21 replies
-
https://react-hook-form.com/ts#UseFormMethods
|
Beta Was this translation helpful? Give feedback.
-
What if I call the component like This causes an error like:
|
Beta Was this translation helpful? Give feedback.
-
as @ptmkenny mentions. Using |
Beta Was this translation helpful? Give feedback.
-
After upgrading to V7, it looks like the way to do this now is: |
Beta Was this translation helpful? Give feedback.
-
Hello @ptmkenny, if you are calling register as a prop in a custom component or in some custom form field in Typescript, then you can use the below code. First of all import the UseFormRegister and FieldValues from 'react-hook-form'
After that, define the type of register as
and boom, you don't need to use 'any' type for register. |
Beta Was this translation helpful? Give feedback.
-
Still have problem :( https://codesandbox.io/s/react-hook-form-control-forked-w39qfq?file=/src/App.tsx |
Beta Was this translation helpful? Give feedback.
-
i'm still getting |
Beta Was this translation helpful? Give feedback.
Hello @ptmkenny, if you are calling register as a prop in a custom component or in some custom form field in Typescript, then you can use the below code.
First of all import the UseFormRegister and FieldValues from 'react-hook-form'
import {UseFormRegister, FieldValues } from 'react-hook-form'
After that, define the type of register as
register: UseFormRegister<FieldValues>
and boom, you don't need to use 'any' type for register.