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: getFieldState(..).error type is incorrect #11814

Closed
1 task done
voinik opened this issue Apr 24, 2024 · 0 comments · Fixed by #11831
Closed
1 task done

issue: getFieldState(..).error type is incorrect #11814

voinik opened this issue Apr 24, 2024 · 0 comments · Fixed by #11831

Comments

@voinik
Copy link

voinik commented Apr 24, 2024

Version Number

7.51.3

Codesandbox/Expo snack

Codesandbox link here

Steps to reproduce

I'm using the Zodresolver ("@hookform/resolvers": "^3.3.4") to do the validation. That might be related.

I've pasted the instructions from the Codesandbox page below. I added a regular string field ("nickname") to the demo as a control to demonstrate that non-array fields work correctly.

Instructions:

  1. Notice how there are no errors starting out

  2. Click "Submit"

  3. Notice how form.getFieldState("files").error?.message is filled. That is because the error is on the array level of the schema (z.arr(..).min(1))

  4. Click "Choose files" and add a file

  5. Notice how form.getFieldState("files").error?.message is no longer filled. It's undefined (see console)

  6. But notice now that form.getFieldState("files").error?.[0]?.message IS filled. Same goes for form.formState.errors.files?.[0]?.message

  7. So getFieldState(..).error should share the signature of formState.errors.x, namely Merge<FieldError, (FieldError | undefined)[]> | undefined. However, it is typed only as FieldError | undefined

  8. Check the code and look for the type error and my comment. You can verify the incorrect types there

Expected behaviour

I expect the type of getFieldState(..).error to be Merge<FieldError, (FieldError | undefined)[]> | undefined, instead of FieldError | undefined. formState.errors.<field>'s type is in fact Merge<FieldError, (FieldError | undefined)[]> | undefined, which I think is the correct type for both. That is because a field's error can be an array of FieldErrors or it could be a single FieldError, as shown in the demo (files and nickname fields, respectively).

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant