issue: Type 'FieldError' is not assignable to type 'string' - type checking error in 7.33.0 #8915
-
Version Number7.33.0 Codesandbox/Expo snackhttps://codesandbox.io/s/dreamy-mountain-6my95x?file=/src/TextField.tsx Steps to reproduce
Alternatively, you can reproduce this error in CRA 5 TypeScript project by copying App.tsx and TextField.tsx from the above sandbox. I ran into this issue while updating my project to the latest version of react-hook-form, and I was able to pinpoint this issue to version 7.33.0 (7.32.2 type-checks just fine). The above sandbox is an over-simplified standalone demo of this error. In this demo I intentionally extracted TextFieldError into a separate component to force the type checking on Expected behaviourNo TypeScript errors as in v7.32.2 What browsers are you seeing the problem on?No response Relevant log outputNo response Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
This is not a bug. The newer version of react-hook-form has been updated with an enhancement on type definition for "errors", before that, "message" field is falling back to "any" type. You just need to update TexFieldError with correct type definition. Working demo: https://codesandbox.io/s/react-hook-form-8914-forked-foorwp?file=/src/TextField.tsx |
Beta Was this translation helpful? Give feedback.
-
@igordanchenko : You're using TextField as a generic component without providing any context about form values to "useFormContext", that's why "errors[name].message" cannot be determined and fallback to all possible supported types. Just put form values for your case (which is a string) to make it work. https://codesandbox.io/s/react-hook-form-8914-forked-cuh5l2?file=/src/TextField.tsx |
Beta Was this translation helpful? Give feedback.
@igordanchenko : You're using TextField as a generic component without providing any context about form values to "useFormContext", that's why "errors[name].message" cannot be determined and fallback to all possible supported types.
Just put form values for your case (which is a string) to make it work.
https://codesandbox.io/s/react-hook-form-8914-forked-cuh5l2?file=/src/TextField.tsx