Skip to content

✨ fix(types): allow undefined value with async defaultValues in Contr…#13160

Merged
bluebill1049 merged 2 commits intoreact-hook-form:masterfrom
Fasping:fix/13125-async-defaultValues-controller-types
Nov 24, 2025
Merged

✨ fix(types): allow undefined value with async defaultValues in Contr…#13160
bluebill1049 merged 2 commits intoreact-hook-form:masterfrom
Fasping:fix/13125-async-defaultValues-controller-types

Conversation

@Fasping
Copy link
Copy Markdown
Contributor

@Fasping Fasping commented Nov 24, 2025

Fixes #13125

Summary

When using useForm with defaultValues: async () => {...}, the first render
provides field.value as undefined, while the TypeScript types currently infer
a non-nullable value. This type mismatch can cause runtime errors if the user
assumes field.value is always defined.

What this PR changes

  • Updates ControllerRenderProps['value'] to include undefined for the initial
    async defaultValues resolution.
  • Adds a dedicated type test for this scenario.
  • Updates existing type tests to reflect the corrected typing.
  • No runtime logic is changed.

Impact

This is a type-level fix that aligns TypeScript with real runtime behavior.
It may expose new TS errors where user code assumed field.value was always
defined, but it prevents runtime crashes and improves type safety.

Tests

  • Type tests updated and passing.
  • Reproduction case verified.
  • No regressions observed.

@bluebill1049 bluebill1049 merged commit ceb0b8f into react-hook-form:master Nov 24, 2025
6 checks passed
@infodusha
Copy link
Copy Markdown

That actually broke types of the values for the case where defaultValues are not async and all defined

Etsija added a commit to eclipse-apoapsis/ort-server that referenced this pull request Dec 1, 2025
[1] adds `undefined` to `ControllerRenderProps['value']` type during async
`defaultValues ` resolution, and this causes some TypeScript errors in our
components.

Fix by
- Adding null coalescing operators for array field values
- Adding early return guards for potentially undefined values
- Refactoring reduce logic for cleaner type inference

[1]: react-hook-form/react-hook-form#13160

Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
Etsija added a commit to eclipse-apoapsis/ort-server that referenced this pull request Dec 1, 2025
[1] adds `undefined` to `ControllerRenderProps['value']` type during async
`defaultValues ` resolution, and this will cause some TypeScript errors in
some components.

Fix by
- Adding null coalescing operators for array field values
- Adding early return guards for potentially undefined values
- Refactoring reduce logic for cleaner type inference

It has been verified that the fixes can be applied before the upgrade
without breaking the build.

[1]: react-hook-form/react-hook-form#13160

Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
sschuberth pushed a commit to eclipse-apoapsis/ort-server that referenced this pull request Dec 1, 2025
[1] adds `undefined` to `ControllerRenderProps['value']` type during async
`defaultValues ` resolution, and this will cause some TypeScript errors in
some components.

In preparation for the upgrade, add null coalescing operators for array
field values and early return guards for potentially undefined values.

[1]: react-hook-form/react-hook-form#13160

Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
github-merge-queue bot pushed a commit to eclipse-apoapsis/ort-server that referenced this pull request Dec 1, 2025
[1] adds `undefined` to `ControllerRenderProps['value']` type during async
`defaultValues ` resolution, and this will cause some TypeScript errors in
some components.

In preparation for the upgrade, add null coalescing operators for array
field values and early return guards for potentially undefined values.

[1]: react-hook-form/react-hook-form#13160

Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
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.

issue: incorrect field type with async defaultValues and Controller

3 participants