Skip to content

fix: add null guards on formState.values in getIn calls (TS2345)#1088

Merged
erikras merged 1 commit intomainfrom
fix/usefield-ts2345
May 5, 2026
Merged

fix: add null guards on formState.values in getIn calls (TS2345)#1088
erikras merged 1 commit intomainfrom
fix/usefield-ts2345

Conversation

@erikras-richard-agent
Copy link
Copy Markdown
Contributor

@erikras-richard-agent erikras-richard-agent commented May 5, 2026

formState.values and form.getState().values are typed as FormValues | undefined in final-form's types, but getIn expects object, causing TS2345 errors at build time. Added null guards at all three call sites in useField.ts.

This fix was missing from the v7.0.1 release PR — needs to land on main so the tag can be updated before npm publish.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced form field state synchronization to safely handle scenarios where field names change before the form state updates. Improved handling of undefined form values to prevent errors and ensure consistent, reliable field value reading and input state management during form operations.

@erikras erikras merged commit 4271873 into main May 5, 2026
3 of 4 checks passed
@erikras erikras deleted the fix/usefield-ts2345 branch May 5, 2026 08:12
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0aafa224-4ce1-4514-8f50-b815bf41d0b2

📥 Commits

Reviewing files that changed from the base of the PR and between 3b98a99 and fbc23b8.

📒 Files selected for processing (1)
  • src/useField.ts

📝 Walkthrough

Walkthrough

This change adds defensive null-checking to useField's initialization and field-synchronization logic. Three locations now use formState.values ?? {} or equivalent to safely handle cases where values is undefined, preventing runtime errors in getIn calls when field state changes occur before form state updates.

Changes

useField Defensive Value Access

Layer / File(s) Summary
Core Logic
src/useField.ts (lines 215, 274–275, 311–312)
Add nullish coalescing fallback ?? {} when reading field values from formState in initial-value effect, getInputValue, and getInputChecked to prevent undefined being passed to getIn.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • final-form/react-final-form#1060: Both PRs modify useField's initial-value and synchronization logic in src/useField.ts, addressing state initialization edge cases.

Suggested reviewers

  • erikras

Poem

🐰 A gentle hop through undefined plains,
Where values once caused painful strains,
Now fallbacks bloom with ?? {} grace,
No more shall null crash state's embrace!

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/usefield-ts2345

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

2 participants