fix: require compatible withFieldGroup field types #2255
main fromrefs/pull/2255/head·opened Fixes #1855
Changes#
- Add field-group-specific deep key helpers that allow parent objects to have extra fields while requiring exposed field values to be compatible.
- Use the stricter helper for
withFieldGroupcomponentfieldsin React, Preact, and Solid. - Document the intentional nullability asymmetry between whole-group field matching and mapped leaf-field matching.
- Add React, Preact, and Solid type regression coverage for reusable groups that can write
nullinto string-only parent fields.
Checklist#
- Added or updated type regression coverage.
- Kept runtime behavior unchanged; this is a type-level compatibility fix.
- Verified affected package type tests locally.
Tests#
pnpm prettier --check packages/form-core/src/util-types.ts packages/react-form/src/createFormHook.tsx packages/react-form/tests/createFormHook.test-d.tsx packages/preact-form/src/createFormHook.tsx packages/preact-form/tests/createFormHook.test-d.tsx packages/solid-form/src/createFormHook.tsxpnpm --filter @tanstack/react-form test:typespnpm --filter @tanstack/preact-form test:typespnpm --filter @tanstack/solid-form test:typespnpm --filter @tanstack/form-core test:types
Release Impact#
TypeScript-only fix for withFieldGroup field compatibility. It should prevent unsafe nullable field-group mappings without changing runtime behavior.
Summary by CodeRabbit#
Enhancements
- Improved TypeScript type checking for field-group field mappings across React, Preact, and Solid integrations.
- Field-group mappings now support compatible nested object and array structures, while preventing unsafe parent/leaf type mismatches.
- Mapping constraints are tightened to avoid cases where a field group could write
null/other incompatible values into a non-compatible parent field.
Tests
- Added TypeScript type-tests covering accepted valid mappings and rejecting incompatible nullable/non-nullable and path-based vs object-based mappings.
- 1
🧹 Nitpick comments (2)
packages/form-core/src/util-types.ts (1)
230-252: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUndocumented asymmetry:
NonNullable<TDeepValue>applied in one helper but not the other.DeepKeysOfFieldGroupTypewraps the deep value inNonNullablebefore the compatibility check, whileDeepKeysOfFieldGroupFieldTypedoes not. This looks intentional (stripping the container's own optionality when matching a whole-object shape viafields="key", while preserving nullability for exact leaf-value matching via mapped fields, e.g. sostring | undefineddeep values can still matchstring | undefinedfield-group leaves), but it's non-obvious and could easily be "fixed" incorrectly by a future refactor unifying the two helpers.A short comment explaining why one strips
NonNullableand the other doesn't would prevent regressions.📝 Suggested doc comment
/** * The keys of an object or array, deeply nested and compatible with a mapped field group value. + * Unlike `DeepKeysOfFieldGroupType`, this does not strip `NonNullable` from the deep value, + * since leaf-level field mappings must preserve exact nullability (e.g. `string | undefined` + * parent fields should still match `string | undefined` field-group fields). */ export type DeepKeysOfFieldGroupFieldType<TData, TValue> = unknown extends TData🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/form-core/src/util-types.ts` around lines 230 - 252, Add a concise explanatory comment near DeepKeysOfFieldGroupType and DeepKeysOfFieldGroupFieldType documenting their intentional asymmetry: the former applies NonNullable to match whole-object field-group containers, while the latter preserves deep-value nullability for exact mapped-field leaf matching. Do not change either type’s behavior.packages/solid-form/src/createFormHook.tsx (1)
494-498: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winChange looks correct; consider parity test coverage for solid-form.
Same constraint fix as react-form/preact-form. The PR only adds regression tests to the React and Preact
test-d.tsxfiles — solid-form gets the identical type-level fix but no equivalentwithFieldGroupnullable-field regression test. Worth adding for parity/future regression safety, though not blocking since the underlyingIsFieldGroupCompatiblelogic is already covered by the other two adapters' tests.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/solid-form/src/createFormHook.tsx` around lines 494 - 498, Add a Solid Form type-level regression test covering withFieldGroup when a field is nullable, matching the equivalent React and Preact test cases. Place it alongside the existing Solid Form test-d.tsx coverage and verify the corrected generic constraint accepts the intended nullable-field configuration.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Nitpick comments: In `@packages/form-core/src/util-types.ts`: - Around line 230-252: Add a concise explanatory comment near DeepKeysOfFieldGroupType and DeepKeysOfFieldGroupFieldType documenting their intentional asymmetry: the former applies NonNullable to match whole-object field-group containers, while the latter preserves deep-value nullability for exact mapped-field leaf matching. Do not change either type’s behavior. In `@packages/solid-form/src/createFormHook.tsx`: - Around line 494-498: Add a Solid Form type-level regression test covering withFieldGroup when a field is nullable, matching the equivalent React and Preact test cases. Place it alongside the existing Solid Form test-d.tsx coverage and verify the corrected generic constraint accepts the intended nullable-field configuration.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID:
15dd92d1-c9ab-4ad8-afca-3dce67ee44bf📥 Commits
Reviewing files that changed from the base of the PR and between 5d1128141a705ebb24ade1275b3117bb4c8b1bdc and 9bdf5e388ac4c3fc994173ea357671253a507a84.
📒 Files selected for processing (6)
packages/form-core/src/util-types.tspackages/preact-form/src/createFormHook.tsxpackages/preact-form/tests/createFormHook.test-d.tsxpackages/react-form/src/createFormHook.tsxpackages/react-form/tests/createFormHook.test-d.tsxpackages/solid-form/src/createFormHook.tsx
Addressed the two CodeRabbit nits in a follow-up.
Changes:
- documented the intentional
NonNullableasymmetry between whole-group and mapped-field deep key helpers - added Solid Form type coverage for nullable
withFieldGroupfield mappings, matching the React/Preact regression shape
Local checks:
pnpm --filter @tanstack/solid-form test:typespnpm --filter @tanstack/form-core test:types
- documented the intentional
xianjianlf2(GitHub profile, opens in a new tab) linked this to fix #1855
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
Field-group deep-key and mapping utilities now evaluate recursive value compatibility. React, Preact, and Solid
withFieldGroupconstraints use these utilities, with type tests covering compatible and incompatible nullable field mappings.Changes#
Field-group typing
packages/form-core/src/util-types.tsFieldGroupFieldsMap.withFieldGroupconstraintspackages/*-form/src/createFormHook.tsxpackages/*-form/tests/createFormHook.test-d.tsxstring | nullmappings and rejects string-only parent mappings in direct and object forms.Estimated code review effort: 3 (Moderate) | ~20 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#1855by tightening withFieldGroup field-map typing and adding regression tests for incompatible nullable mappings.✨ Finishing Touches
🧪 Generate unit tests (beta)
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