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: data param in onValid in handleSubmit infers as any when TTransformedValues is a union type #11414

Closed
1 task done
Ar4ys opened this issue Jan 19, 2024 · 3 comments · Fixed by #11420
Closed
1 task done
Labels
TS Typescript related issues

Comments

@Ar4ys
Copy link
Contributor

Ar4ys commented Jan 19, 2024

Version Number

7.49.3

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/youthful-chaplygin-4svfw9

Steps to reproduce

  1. Go to CodeSandbox
  2. Open /src/App.tsx file
  3. In useTTransformedValues in handleSubmit (in line 9) you will see that:
    • data is inferred as any
    • as a result, TS shows an error: "Parameter 'data' implicitly has an 'any' type."

Expected behaviour

data should be inferred as Form (i.e. union type) - the same as in useOnlyTFieldValues (line 14)

What browsers are you seeing the problem on?

No response

Relevant log output

Parameter 'data' implicitly has an 'any' type.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Ar4ys
Copy link
Contributor Author

Ar4ys commented Jan 19, 2024

This issue happens because of Distributive Conditional Types feature in TypeScript. Specifically, because of how TTransformedValues is defined and used inside of UseFormHandleSubmit: github permalink.

There are two possible fixes I came up with:

  1. Force TypeScript to avoid distributing conditional types "by surrounding each side of the extends keyword with square brackets" inside UseFormHandleSubmit (taken from TypeScript Handbook#Distributive Conditional Types)
  2. Replace current UseFormHandleSubmit implementation with the one that relies on default values in generics:
// Proposed implementation of `UseFormHandleSubmit`
export type UseFormHandleSubmit<
  TFieldValues extends FieldValues,
  TTransformedValues extends FieldValues = TFieldValues,
> = (
  onValid: SubmitHandler<TTransformedValues>,
  onInvalid?: SubmitErrorHandler<TFieldValues>,
) => (e?: React.BaseSyntheticEvent) => Promise<void>;

The second solution should work in all use-cases, that current one covers without any drawbacks (at least I could not come up with any use-case that may break). As such I would like to create a PR with second fix applied, but as a safety measure I would like to know the reasoning behind current TTransformedValues and UseFormHandleSubmit implementation (maybe second fix actually breaks in some obscure edge case).

@bluebill1049 bluebill1049 added the TS Typescript related issues label Jan 20, 2024
@bluebill1049
Copy link
Member

Replace current UseFormHandleSubmit implementation with the one that relies on default values in generics:

Could you share a PR with the second approach?

@Ar4ys
Copy link
Contributor Author

Ar4ys commented Jan 20, 2024

Here is PR #11420 with the second approach.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
TS Typescript related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants