I want to pass the function setValue() down to a child component. Then I get the following error message:
Type 'UseFormSetValue<Inputs>' is not assignable to type 'UseFormSetValue<Record<string, any>>'

How can I pass down the function correctly?
Sandbox example
function App() {
const {
register,
setValue,
} = useForm<Inputs>({
});
return (
<form>
<Field2 setValue={setValue} register={register} />
<input type="submit" />
</form>
);
}
The default UseFormReturn["setValue"] type resolves to UseFormSetValue<Record<string, any>>. You can explicitly configure the setValue type to use the correct generic params like this:
setValue: UseFormSetValue<Inputs>;
Instead of:
setValue: UseFormReturn["setValue"];
Note: All public types can be imported from react-hook-form module
import {
UseFormReturn,
UseFormSetValue
} from "react-hook-form";
I also fixed your related type error in register props in the live demo (UseFormRegister<Inputs> instead of UseFormReturn['register']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With