Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue: useWatch<T>() (parameterless) and watch type from the useFormContext have different types #10734

Closed
1 task done
Zetxus opened this issue Jul 31, 2023 · 2 comments · Fixed by #11359
Closed
1 task done
Labels
TS Typescript related issues

Comments

@Zetxus
Copy link

Zetxus commented Jul 31, 2023

Version Number

7.43.9

Codesandbox/Expo snack

https://codesandbox.io/s/adoring-shadow-zgr3rd?file=/src/App.tsx

Steps to reproduce

Recreated from #10721 using the template as the GitHub "new issue" from discussion functionality skips your templates.

  1. Go to Example1
  2. See the types comparison between useWatch<Form>() and useFormContext<Form>()
  3. Go to Example2
  4. See the workaround that gets rid of the deep partial

Description:
Using useWatch<T>() (without providing any parameters) returns a deep partial unlike the watch type from useFormContext. This results in having to either cast the result to the non-partial value (T) or @ts-ignore the type in order to get my defined type instead of the deep partial one.

I can't actually just use the useFormContext#watch because that results in infinite updates when I try to use it the same way I use useWatch and gives me performance issues so I've moved away from using it even though the typing there is what I need.

You can see the Example2 file for the usage of a nested property to avoid getting deep partials.

Expected behaviour

I expect that the types would be the same between the two. Unless it's some design decision for some specific case, to me it doesn't make sense for it to be a deep partial.

What browsers are you seeing the problem on?

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@SebDelile
Copy link

SebDelile commented Sep 18, 2023

Hello,
I ran into the same issue, thanks for the work around ! it does the job for now but a fix would be appreciate :)

EDIT: my mistake I didn't see you had to modify the form defaultValues to make the workaround to work, so it's less easy to use than I'd figured it out at the first look.

RE-EDIT: there is a simplier workaround ! taking the Example 1 from the Sandbox:

Replace
const watchData = useWatch<Form>(); // watchData has deep partial type
with
const watchData = useWatch() as Form; // watchData is well typed

@Zetxus
Copy link
Author

Zetxus commented Sep 27, 2023

Hey @SebDelile sorry for the delayed response.

I didn't actually consider using as because it kind of "forces" your type to comply by casting it. In my experience that usually results in other problems in the future.

That's why I went with increasing the depth of the properties instead, but it would be cool if react-hook-form could handle this without the need to modify the original data format (as you mentioned that could be uncomfortable to work with).

bluebill1049 added a commit that referenced this issue Jan 13, 2024
Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
bluebill1049 added a commit that referenced this issue Feb 6, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
rafaelcalhau pushed a commit to rafaelcalhau/react-hook-form that referenced this issue May 5, 2024
…turn type (react-hook-form#11359)

Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
rafaelcalhau pushed a commit to rafaelcalhau/react-hook-form that referenced this issue May 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
TS Typescript related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants