getValues() doesn't represent provided form generic #11724
-
Beta Was this translation helpful? Give feedback.
Answered by
sujinleeme
Mar 29, 2024
Replies: 1 comment 4 replies
-
Which react hook form do you use now? The defaultValue type uses DeepPartial, which means all properties of an existing type(form value) are optional. react-hook-form/src/types/form.ts Line 47 in 306627e The latest version(v7) has no problem with defining the FormData type const { getValues } = useForm<{
foo: string;
}>({
mode: "all",
defaultValues: {
foo: undefined,
},
});
// You may need to use the optional chaining operator because the value can be undefined.
const fooLength = getValues().foo?.length; |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, it is expected because you provide the generic.
react-hook-form/src/types/form.ts
Line 837 in ebc1f2c
If you remove the generic type, react hook form will infer the type based on
defaultValues
In my experience, the best way to solve this type of mismatching with the generic is just providing
{foo: ''}
if the field is a simple input component. Then you can avoid this.