react-hook-form: V7 Bug - Typescript performance issue on Field Array

Describe the bug

If

  1. A custom type is created to represent form values
  2. The type is of a certain structure
  3. The type is used as a generic in useForm,

then typescript compilation will slow down tremendously.

In larger apps with many useForms that take this generic, Node can run out of heap memory even with 8gb of allocated heap. I’ve created a code sandbox, but unfortunately, the issue can not really be observed there since we cannot track typescript compilation time (at least to my knowledge). Therefore, I’ve created a very simple sample project to show the issue.

To Reproduce Steps to reproduce the behavior:

  1. Download the sample project.
  2. Run npm/yarn install
  3. Run npm/yarn start
  4. Observe the extremely long time it takes for typescript to compute results via react-scripts latest version

Codesandbox link (Required) Use this memory-issues.zip

might not help but included regardless. https://codesandbox.io/s/summer-sunset-b2wwn?file=/src/App.tsx

Desktop (please complete the following information):

  • OS: Ubuntu 20.04 LTS

Additional Context I copied over my types exactly. The issue is focused around using an object in a triple nested array. So line 51 in types.ts. If you remove the custom select type here and just use null, then the issue is improved exponentially.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 42 (42 by maintainers)

Commits related to this issue

Most upvoted comments

@wdfinch

The old one allows any child item which is an array in a nested object might have length “0” -> “99” while the new one allows “0” -> “49” of length.

@bluebill1049

I’ve changed the type definition in line 66 of types/utils.ts

https://github.com/react-hook-form/react-hook-form/blob/v7/src/types/utils.ts#L60-L66

It was:

type ArrayKey = number | Digits | `${OneToFourDigits}${Digits}`;

I’ve tested OK without any performance issue on new sample of @wdfinch

@bluebill1049

Since Typescript 4.1 allows to use template literal type by using primitive type, can we use:

declare type ArrayKey = number | `${number}`;

instead of

type ArrayKey = number | Digits | `${OneToFourDigits}${Digits}`;

Do you see any issue if we switch into this one ?

Sounds good. Hopefully, it can be done. I appreciate you helping me with this issue.

@wdfinch feel free to take a look at our ts definition: https://github.com/react-hook-form/react-hook-form/tree/v7/src/types see if anything you can pick up for the improvement.