react-hook-form: IntelliSense hangs infinitely in VSCode after installing react-hook-form
Version Number
IntelliSense to hang infinitely in VSCode
Codesandbox/Expo snack
7.52.2
Steps to reproduce
- Install the latest
react-hook-form - Try triggering IntelliSense autocompletion with control + space (on mac):
import React, { useEffect, [CONTROL + SPACE HERE] } from "react";
- IntelliSense hangs infinitely: “Loading…”
Expected behaviour
IntelliSense should work.
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
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 10
- Comments: 18
I spent quite a bit of time investigating this after an issue was opened over on Zod: https://github.com/colinhacks/zod/issues/3233
A few takeaways:
This is often user error
There is rarely a reason to be passing a type into
useFormthat is so wide/deep/recursive as too break the existingPath<>logic in RHF. I think most occurrences of this issue are user error, naively dropping in huge generated types intouseForm. You should usePick/Omitto pare down a complex type something that actually represents the data your form expects.Depth limit
That said, I think a depth limit might be a good idea. This will save headaches for people who unintentionally pass in huge types, and would allow RHF to handle recursive types pretty elegantly. It would be valuable to get some guidance from maintainers on how they’d like this implemented.
a) The depth limit would need to be exposed to the users via
useForm. Perhaps as a field insideTContext?useForm<MyData, { depth: 5 }>(...)Then the depth could get passed along as part of the generics inControl<>and applied when passed intouseController/Controller.b) Default values. You don’t want to break too many existing projects but a default depth of 50 (as in the existing PR #12169) seems high. Hard to imagine any reasonable form with a depth above, say, 10? (In the Zod issue I referenced earlier a depth of 7 was already enough to freeze VS Code.)
c) Removing
TraversedTypeslogic. Once there is a depth limit in place, there’s less reason to keep theTraversedTypeslogic around. The type comparisons (AnyIsEqual<TraversedTypes, V>) are the most expensive part of thePath<>logic by orders of magnitude. If you get rid of it and go “all in” on a depth-based approach RHF is able to generate even large numbers of paths (thousands) very fast.Another approach: path builder
I have a concept for an alternative approach that may solve the problem more elegantly: a “path builder” pattern. It’s nice because it lets your type logic stay shallow, instead of trying to recursively compute all paths upfront.
You can get a sense of what I mean from my utility library
trailhead: https://www.npmjs.com/package/trailheadIn the context of RHF, this is the API I’d recommend.
This
.path()method returns a “builder” object that lets build a path progressively using chained dot/index accesses.controlas well, which means there would be no need to passcontrolas a separate prop, it could be inferred vianame.Backwards compatibility
This whole proposal could be done in a backwards compatible way. The
namefield could be updated to accept either a regular string path or a “path builder” instance. It’s worth noting that this approach requiresProxy(see the implementation oftrailheadfor an idea of how it works).I think this approach is complementary to a depth limit as well. A depth limit of ~6 would effectively prevent TS performance issues. Anyone who needs deeper paths could use the path builder.
If the maintainers are interested in this at all I’m happy to put in a PR. We can flesh out details in the PR comments.
Took me 3 days to find this problem. If I remove the type casting from useForm, then it’s fixed. Turns out it takes the entire power of my laptop to define
typeof RequestFundsFormSchema.Unending pain version:
Happy and fast version:
@Rohit10701 I’ve opened a PR, but your can also try applying this patch (tweak
DefaultDepthif type checking is too slow or if you get type errors):@KKrisu they would need to rewrite the type. Fundamentally the
key string || numberthat supports both indexing and records is flawed and cannot be fixed.Some have suggested a max depth, but that fails when the breadth gets to large as well since it greedily evaluates the type.
So the issue here is with TSC itself. The type they are using to automagically do the string key is not going to work, intellisense loses it for me when I try a structure like this:
{dog: Array<{item: string}>}If you try to
dog.${0}.itemitem won’t intellisense for me which makes sense because the type in their system is{number}whereas 0 is 0. You can try to rewrite this with string lits and this will fix this issue.This is not the first time I’ve encountered this issue in TSC and the problem stems from the recursive type having to build every possible string for every possible key and mixing Array indexes with Object symbol/number/string. Don’t do this.
The real solution is using a method that works with TSC, and that is Lenses/Optics. These don’t give a care about what exists inbetween because you prove it at each step. TSC only has to confirm your path rather than every single path in existence ever for the entire object.
How this would work would be Lens<A, S> instead of “key.that.this”, but the key here is that Lenses can also handle the indexing without losing intellisense since indexing into an Array is special and Lenses treat it as such.
Whereas this type in this library tries to handle every case. Those types are just asking “how sure are you covered every possible type”, throw something exotic in there where there’s no special handling for it and you’ll get the keys of the object now mixed in with all the keys you want and then you end up with breadth and depth of type instantiation.
for time being i would just just apply a patch or comment the file in node_modules manually, that’s how i am working rn, it ll mess up some of the type but its not that worse, unless ur project is super complex
checkout how i did it : https://github.com/Rohit10701/tac-form/tree/master/packages/tac-form/patches
@pax-k Thanks for opening this issue, I wouldn’t have found it otherwise, this has been causing me a headache. Last version without this bug as far as I can tell is
v7.41.5, was introduced inv7.42.0, just downgraded it while it’s getting fixed.EDIT: It began giving me
Type instantiation is excessively deep and possibly infinite.errors. Turns out I’ve givenuseForma type that was too complex to handle. Finally, it got fixed by simplifying my form.