TypeError: Type '"day"' does not satisfy the constraint 'Path<TFormValues>' #11170
Unanswered
pranavdhamanage-work
asked this question in
General
Replies: 1 comment
-
I have defined reusable components with the following code. I'm glad if it helps. type AccountSchema = {
name: string;
day: number;
};
type BirthDateSchema = {
day: number;
};
type BirthDateFieldsProps<T> = T extends BirthDateSchema
? {
control: Control<T>;
}
: never;
const BirthDateFields = <T extends BirthDateSchema>({
control,
}: BirthDateFieldsProps<T>) => {
return <input {...control.register('day')} />;
};
const AccountForm = () => {
const { control } = useForm<AccountSchema>();
return <BirthDateFields control={control} />;
};
const BirthDateForm = () => {
const { control } = useForm<BirthDateSchema>();
return <BirthDateFields control={control} />;
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Error:
Type '"day"' does not satisfy the constraint 'Path'. ts(2344)
Description:
control
prop externally from useForm.useController
hook. But when we provide generic type touseController
first type argument and a value toname
key in object provided touseController
hook... ts compiler throws the error : Type '"day"' does not satisfy the constraint 'Path'. ts(2344)Sandbox link:
https://codesandbox.io/s/reusable-react-hook-form-component-with-typescript-generics-forked-x43fd7?file=/src/SimpleForm.tsx
Code:
The error we get on 'day' is : Type '"day"' does not satisfy the constraint 'Path'. ts(2344)
I also tried extending TFormValues from FieldValues provided by react hook form shown below:
Please let me know what am I missing in this code or what is actually going wrong. I am not able to pinpoint the issue.
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions