Skip to content

fix: deepkeyandvalue behavior in union types#2057

Open
mdm317 wants to merge 4 commits intoTanStack:mainfrom
mdm317:fix/union-types-in-deepkeysandvalues
Open

fix: deepkeyandvalue behavior in union types#2057
mdm317 wants to merge 4 commits intoTanStack:mainfrom
mdm317:fix/union-types-in-deepkeysandvalues

Conversation

@mdm317
Copy link

@mdm317 mdm317 commented Mar 3, 2026

🎯 Changes

fixes #1813

Fix DeepKeysAndValues to return nullish values instead of never when the value type is nullish

  • main
type Test = DeepKeysAndValues<{ id : undefined }>
//        ^? never

ts playground

  • this branch
type Test = DeepKeysAndValues<{ id : undefined }>
//        ^? { key : 'id', value: undefined}

Due to the above change, I replaced DeepKeysOfType with DeepKeysOfNonNullableType in the group API.

#2057 (comment)

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Change

@changeset-bot
Copy link

changeset-bot bot commented Mar 3, 2026

🦋 Changeset detected

Latest commit: 81806f8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@tanstack/react-form Patch
@tanstack/solid-form Patch
@tanstack/form-core Patch
@tanstack/react-form-nextjs Patch
@tanstack/react-form-remix Patch
@tanstack/react-form-start Patch
@tanstack/angular-form Patch
@tanstack/form-devtools Patch
@tanstack/lit-form Patch
@tanstack/svelte-form Patch
@tanstack/vue-form Patch
@tanstack/react-form-devtools Patch
@tanstack/solid-form-devtools Patch

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

Comment on lines +201 to +202
? [NonNullable<V>] extends [never]
? never
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the previous behavior inferred never when the value was nullish,
the changes were updated to continue inferring never for nullish value types.

Comment on lines +208 to +209
export type DeepKeysOfNonNullableType<TData, TValue> =
ExtractByNonNullableValue<DeepKeysAndValues<TData>, TValue>['key']
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

Union types are not handled correctly

1 participant