Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type is register from react-hook-form?

When using react-hook-form with Typescript, there is a component that sends some props, register being one of them.

The issue is with its type when declared in an interface:

export interface MyProps {
  title: string;
  ...
  register: (string | undefined) => void;
}

Which is the correct way of declaring register here?

Also tried with:

import { RegisterOptions } from 'react-hook-form';
    export interface MyProps {
      title: string;
      ...
      register: RegisterOptions;
    }
like image 915
Leo Messi Avatar asked Jan 22 '26 14:01

Leo Messi


2 Answers

if you are calling register as a prop in a custom component or to use in some custom input 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.

like image 140
Ritik Banger Avatar answered Jan 24 '26 03:01

Ritik Banger


Try this.

import { RegisterOptions, UseFormRegisterReturn } from 'react-hook-form';

export interface MyProps {
  title: string;
  ...
  register: (name: string, options?: RegisterOptions) => UseFormRegisterReturn;
}
like image 24
endmaster0809 Avatar answered Jan 24 '26 04:01

endmaster0809



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!