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 |
|
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)
📝 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)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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.
🎯 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