You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The issue of: Type 'string' is not assignable to type '${string} | ${string}.${string} | ${string}.${number}'. #4748
Describe the bug
We followed the v6 to v7 migration docs and are still running into this error in several areas where we're using react-hook-form.
We have generic components in custom libraries that have typed props like fieldName: string or arrayIndex: number. In v7, use of variables like these result in the TS error: Type 'string' is not assignable to type '${string} | ${string}.${string} | ${string}.${number}'.. Using as const doesn't make the TS error go away. Changing the prop type to any doesn't fix it either.
We get the error when trying to use setError() with a field name parameter as mentioned. We also get this error when trying to set dynamic field names using useFieldArray() (for example) with a typed index parameter. So something like arrayName.${index}.${fieldName} on a Controller name property or input register name. Again, neither can be fixed in my cause by using as const.
To Reproduce
Steps to reproduce the behavior: see Codesandbox
Codesandbox link (Required)
In the linked CodeSandbox, you'll see the typed variable causes the TS error. The literal parameter does not. I don't have the option to use a literal (non-typed) parameter because it's a typed prop provided to the component. [Edit] Added setError examples as well. https://codesandbox.io/s/sleepy-voice-30pym?fontsize=14&hidenavigation=1&theme=dark
Expected behavior
No TypeScript error.
Desktop (please complete the following information):
@kg-currenxie That wasn't even working for me. Unless the variable was a literal, TypeScript doesn't like it. So an incoming typed property, or a calculated/dynamic value wouldn't work.
Activity
bluebill1049 commentedon Apr 12, 2021
I think you are after this gets merged in TS: microsoft/TypeScript#43361
bluebill1049 commentedon Apr 13, 2021
closing this issue as this will be resolved when the new TS version is released. (it's already been merged into master)
kg-currenxie commentedon Apr 26, 2021
Then why was it released here in the first place? 🤔
v6
Is this how we're supposed to make it work in v7?
Kizmar commentedon Apr 26, 2021
@kg-currenxie That wasn't even working for me. Unless the variable was a literal, TypeScript doesn't like it. So an incoming typed property, or a calculated/dynamic value wouldn't work.
kg-currenxie commentedon Apr 26, 2021
Seems to work fine here. TypeScript 4.1.2
Its just a wrapper component where i pass the name down, as a string