Skip to content

Fix TTransformedValues inference for #12638#12664

Closed
controversial wants to merge 2 commits into
react-hook-form:feature/infer-resolver-output-typesfrom
controversial:feature/infer-resolver-output-types
Closed

Fix TTransformedValues inference for #12638#12664
controversial wants to merge 2 commits into
react-hook-form:feature/infer-resolver-output-typesfrom
controversial:feature/infer-resolver-output-types

Conversation

@controversial

@controversial controversial commented Mar 6, 2025

Copy link
Copy Markdown

cc @jorisre

fixes types as part of #12638

Why

  • Using ExtractTransformedValues<> from (typeof props)['resolver'] does not work because within the implementation signature, the value in typeof props does not capture any specific type information that is not a part of the useForm function’s generic type parameters.
  • Since useForm is only generic over TFieldValues/TContext/TTransformedValues, there is no type information to be gained from (typeof props)['resolver'] because the compiler will never see the type (typeof props)['resolver'] as anything more specific than Resolver<TFieldValues, TContext, TTransformedValues>.
  • Therefore, we should just use TTransformedValues
    • The compiler already checks that the type of resolver matches TTransformedValues
    • moreover, the compiler can already infer the type of TTransformedValues based on the type of props.resolver, without using the broken ExtractTransformedValues type helper

Effect

This PR fixes the broken types in the handleSubmit tests in react-hook-form/resolvers#753

Before, form is erroneously inferred with a TTransformedValues parameter of any, and the test fails
Screenshot 2025-03-06 at 1 21 19 PM

After, form infers the correct type for TTransformedValues and there is no type error; the test passes
Screenshot 2025-03-06 at 1 23 15 PM

@bluebill1049

Copy link
Copy Markdown
Member

aweomse thanks! could you update the API extractor?

@controversial

Copy link
Copy Markdown
Author

Now updated; sorry for the delay! @bluebill1049

@jorisre

jorisre commented Mar 12, 2025

Copy link
Copy Markdown
Member

@controversial Thanks for your assistance. Could you please take a look at PR #12638 when you get a chance?

@controversial

Copy link
Copy Markdown
Author

Merged into #12638 as part of e7e611e

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 this pull request may close these issues.

3 participants