I am working with a react-hook-form
component:
interface FormFieldInputLabelProps<
TFieldValues extends FieldValues = FieldValues,
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
> {
control: Control<TFieldValues>;
name: TFieldName;
}
export function FormFieldInputLabel<
TFieldValues extends FieldValues = FieldValues,
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
>({ name, control }: FormFieldInputLabelProps<TFieldValues, TFieldName>) {
const { label } = useFieldContext();
const { field } = useController({
control,
name,
defaultValue: label
});
return <input type="hidden" {...field} />;
};
Although it functions correctly, I encountered an issue when trying to assign the label
(a string) as the defaultValue
of the field. The error message displayed is:
Type 'string' is not assignable to type 'UnpackNestedValue<PathValue<TFieldValues, TFieldName>>'.ts(2322)
It seems that there may be uncertainty regarding whether
UnpackNestedValue<PathValue<TFieldValues, TFieldName>>
will always be a string... Is there a way to ensure that the provided name
indeed corresponds to a value of type string
?
Access the codesandbox demonstrating the warning and intended usage here:
https://codesandbox.io/s/condescending-kare-38kdzm?file=/src/App.tsx