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

Type error about async validate function #367

Closed
FuJuntao opened this issue Oct 13, 2019 · 7 comments
Closed

Type error about async validate function #367

FuJuntao opened this issue Oct 13, 2019 · 7 comments
Labels
question Further information is requested

Comments

@FuJuntao
Copy link

Describe the bug
When passing an async function to register's validate rule, there would be a type error. But dispite the type check result, js could run well.

To Reproduce
Steps to reproduce the behavior:

  1. Pass any async function to register's validate rule in tsx file

Typescript message
No overload matches this call.
Overload 1 of 2, '(validateRule: Partial<{ required: string | boolean; min: string | number | { value: ReactText; message: string; }; max: string | number | { value: ReactText; message: string; }; maxLength: string | number | { ...; }; minLength: string | ... 1 more ... | { ...; }; pattern: RegExp | { ...; }; validate: Validate | ... 1 more ... | { ...; }; }>): (ref: any) => void', gave the following error.
Type '(value: any) => Promise<"Please enter less than 3 letters" | undefined>' is not assignable to type 'Validate | Record<string, Validate> | { value: Validate | Record<string, Validate>; message: string; } | undefined'.
Type '(value: any) => Promise<"Please enter less than 3 letters" | undefined>' is not assignable to type 'Validate'.
Type 'Promise<"Please enter less than 3 letters" | undefined>' is not assignable to type 'ValidateResult'.
Type 'Promise<"Please enter less than 3 letters" | undefined>' is not assignable to type 'void'.
Overload 2 of 2, '(ref: ElementLike | null, validationOptions?: Partial<{ required: string | boolean; min: string | number | { value: ReactText; message: string; }; max: string | number | { value: ReactText; message: string; }; maxLength: string | ... 1 more ... | { ...; }; minLength: string | ... 1 more ... | { ...; }; pattern: RegExp | { ...; }; validate: Validate | ... 1 more ... | { ...; }; }> | undefined): void', gave the following error.
Argument of type '{ required: string; validate: (value: any) => Promise<"Please enter less than 3 letters" | undefined>; }' is not assignable to parameter of type 'ElementLike'.
Object literal may only specify known properties, and 'required' does not exist in type 'ElementLike'.

Additional context
Maybe it's because type Validate in src/types.ts isn't defined properly?

@FuJuntao
Copy link
Author

FuJuntao commented Oct 13, 2019

"react-scripts": "3.2.0",
"typescript": "3.6.4",
"react-hook-form": "^3.23.18",

@bluebill1049
Copy link
Member

Can u put together a codesandbox? So we can easily reproduce?

@FuJuntao
Copy link
Author

Can u put together a codesandbox? So we can easily reproduce?

Yes, of course. Here is the codesandbox link.

@bluebill1049
Copy link
Member

thanks for the codesandbox link :) this is fixed and will be patched in the release <3

@bluebill1049 bluebill1049 added the bug Something isn't working label Oct 14, 2019
bluebill1049 added a commit that referenced this issue Oct 14, 2019
@JJ-DiazBaquero
Copy link

This issue is not completely solved, we still got an error when using an async function that returns either a string or a boolean, the type Promise<string | boolean> might be missing for ValidateResult :)

@bluebill1049 bluebill1049 reopened this Nov 14, 2019
@bluebill1049
Copy link
Member

export type ValidateResult =
  | string
  | boolean
  | void
  | Promise<string>
  | Promise<boolean>;

hey @jj-diaz1067 that's what we have right now. I guess it should work, feel free to send a PR if you have the solution.

@bluebill1049 bluebill1049 added question Further information is requested and removed bug Something isn't working labels Nov 14, 2019
@bluebill1049
Copy link
Member

close again :) feel free to submit a PR or let me know why it's not working for you. @jj-diaz1067

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants