react-hook-form / input

📋 Wrapper component for controlled inputs
https://react-hook-form.com/api#Controller
158 stars 14 forks source link

RHFInput type issue with setValue #46

Closed Atralbus closed 4 years ago

Atralbus commented 4 years ago

I got TS2322 error because of the type mismatch with useForm. This issue started when react-hook-form made setValue typesafe:

Type '<Name extends "firstName" | "lastName">(name: Name, value: FormData[Name], shouldValidate?: boolean | undefined) => void | Promise<...>' is not assignable to type '(name: string, value: any, trigger?: boolean | undefined) => void'.

The signature for setValue in useForm:

setValue: <Name extends FieldName<FormValues>>(name: Name, value: FormValues[Name], shouldValidate?: boolean | undefined) => void | Promise<boolean>

The signature for setValue in RHFInput:

setValue?: (name: string, value: any, trigger?: boolean) => void;

I thought RHFInput could use name property to define the type of the argument in setValue, but I'm not sure how it could be handled for the type of the value argument (FormData[Name]). I guess it wouldn't cause a type error if it remains as any.

Is it possible to make setValue's name argument typesafe? Or do you have a suggestion for handling this type issue without casting or having a lot less type safety?

typescript: v3.6.3

bluebill1049 commented 4 years ago

hey @Atralbus sorry for the late reply. RHFInput will be moved into the main repo of RHF. This issue should be resolved by that. I will keep you updated when we release it.

Atralbus commented 4 years ago

I see. Looking forward to it.

bluebill1049 commented 4 years ago

@Atralbus we have released V4, try it out with our new Controller component :) and merry xmas

bluebill1049 commented 4 years ago

https://react-hook-form.com/api#Controller