fix: deepkeyandvalue behavior in union types#2057
Conversation
🦋 Changeset detectedLatest commit: 81806f8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| ? [NonNullable<V>] extends [never] | ||
| ? never |
There was a problem hiding this comment.
Since the previous behavior inferred never when the value was nullish,
the changes were updated to continue inferring never for nullish value types.
| export type DeepKeysOfNonNullableType<TData, TValue> = | ||
| ExtractByNonNullableValue<DeepKeysAndValues<TData>, TValue>['key'] |
There was a problem hiding this comment.
After changing DeepKeysAndValues to include nullish values, a bug was introduced in the group API when using DeepKeysOfType.
When the value type is nullish, it previously evaluated as:
Extract<never, nullish | TValue>
However, after the change, it became:
Extract<nullish, nullish | TValue>
which now returns nullish.
To resolve this issue, I implemented a new utility type to correctly infer TFields in the group API.
|
The code looks good to me and makes sense, but given that @LeCarbonator has to remind me every week how FieldGroup actually works, I'm going to pass it to him 😅 Pinged internally so response time is usually quicker that way. |
LeCarbonator
left a comment
There was a problem hiding this comment.
Looks interesting! Reported cause and the reasoning for the change make sense to me! As far as the changes go, there's only some regression tests missing as well as an instantiation check. Refer to the two open threads for more details.
Thanks for tackling this!
| ? TAcc | UnknownDeepKeyAndValue<TParent> | ||
| : T extends object | ||
| ? DeepKeyAndValueObject<TParent, T, TAcc> | ||
| : TAcc |
There was a problem hiding this comment.
This particular change addresses not just DeepKeysOfType, but also DeepKeys and DeepValue.
I see one test was adjusted to acknowledge this, but some additional LOC for DeepKeys and DeepValue would be fantastic. Not merely for this PR, but also for future typings to follow the spec.
There was a problem hiding this comment.
Thanks for review!
I added case in this commit 2ee891c
There was a problem hiding this comment.
How's type instantiation treating this type change? Have you tested them before / after? Did they explode or is it only a bit more?
I would be surprised if it did, but you never know with some of those seemingly simple changes.
If you haven't already, I can help out with testing it. Just let me know.
There was a problem hiding this comment.
Measured with the following command.
Instantiations increased slightly on this branch's HEAD c0d762dd compared to base 3886dcc5.
npx tsc --extendedDiagnosticspackages/form-core
| metric | base | HEAD |
|---|---|---|
| Types | 84,335 | 86,803 |
| Instantiations | 436,577 | 449,014 |
packages/react-form
| metric | base | HEAD |
|---|---|---|
| Types | 58,924 | 60,824 |
| Instantiations | 435,282 | 447,536 |
packages/preact-form
| metric | base | HEAD |
|---|---|---|
| Types | 51,525 | 52,567 |
| Instantiations | 322,435 | 326,877 |
packages/vue-form
| metric | base | HEAD |
|---|---|---|
| Types | 25,531 | 26,099 |
| Instantiations | 113,263 | 114,659 |
packages/solid-form
| metric | base | HEAD |
|---|---|---|
| Types | 37,855 | 39,301 |
| Instantiations | 203,009 | 210,621 |
There was a problem hiding this comment.
That's only a minor amount because this would only account for a few unit tests.
I took the liberty to give it a try in my work space. We use TanStack Form a lot there and we have way more types, so the impact should be a bit clearer.
packages/react-form
| metric | v1.28 | HEAD | Diff (~) |
|---|---|---|---|
| Types | 439'839 | 439'089 | - 0.17% |
| Instantiations | 2'983'698 | 2'978'088 | - 0.18% |
| Memory | 1'406'720K | 1'412'563K | + 0.41% |
Looks clean!
There was a problem hiding this comment.
Thanks for checking — appreciate it.
Let me know if there’s anything else I should work on.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAdds a never-base case to deep-key recursion, introduces DeepKeysOfNonNullableType to filter deep-key paths by non-nullable accessor values, updates field-group generic constraints across form-core/react/solid/preact to use it, and adds type tests plus a changeset documenting the patch releases. ChangesUnion Type Handling in Field Groups
🎯 3 (Moderate) | ⏱️ ~25 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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. Comment |
| TFieldGroupData, | ||
| TFields extends | ||
| | DeepKeysOfType<TFormData, TFieldGroupData | null | undefined> | ||
| | DeepKeysOfNonNullableType<TFormData, TFieldGroupData> |
There was a problem hiding this comment.
Apply the same non-nullable field group fix to preact-form.
|
View your CI Pipeline Execution ↗ for commit c0d762d
☁️ Nx Cloud last updated this comment at |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2057 +/- ##
==========================================
+ Coverage 90.35% 91.27% +0.91%
==========================================
Files 38 59 +21
Lines 1752 2314 +562
Branches 444 569 +125
==========================================
+ Hits 1583 2112 +529
- Misses 149 181 +32
- Partials 20 21 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
🎯 Changes
fixes #1813
Fix DeepKeysAndValues to return nullish values instead of never when the value type is nullish
ts playground
Due to the above change, I replaced DeepKeysOfType with DeepKeysOfNonNullableType in the group API.
✅ Checklist
pnpm test:pr.🚀 Release Impact
Change
Summary by CodeRabbit
Bug Fixes
Tests
Chores