-
-
Notifications
You must be signed in to change notification settings - Fork 645
fix: deepkeyandvalue behavior in union types #2057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bc15ad2
9e4fc52
e57ffb3
81806f8
58376b1
2ee891c
c0d762d
7bcae35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@tanstack/react-form': patch | ||
| '@tanstack/solid-form': patch | ||
| '@tanstack/form-core': patch | ||
| --- | ||
|
|
||
| Fix `DeepKeysAndValues` to correctly handle union types whose values include nullish types and preserve them in the resulting value type |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,21 +152,23 @@ export type DeepKeysAndValuesImpl< | |
| T, | ||
| TParent extends AnyDeepKeyAndValue = never, | ||
| TAcc = never, | ||
| > = unknown extends T | ||
| ? TAcc | UnknownDeepKeyAndValue<TParent> | ||
| : unknown extends T // this stops runaway recursion when T is any | ||
| ? T | ||
| : T extends string | number | boolean | bigint | Date | ||
| ? TAcc | ||
| : T extends ReadonlyArray<any> | ||
| ? number extends T['length'] | ||
| ? DeepKeyAndValueArray<TParent, T, TAcc> | ||
| : DeepKeyAndValueTuple<TParent, T, TAcc> | ||
| : keyof T extends never | ||
| ? TAcc | UnknownDeepKeyAndValue<TParent> | ||
| : T extends object | ||
| ? DeepKeyAndValueObject<TParent, T, TAcc> | ||
| : TAcc | ||
| > = [T] extends [never] | ||
| ? TAcc | ||
| : unknown extends T | ||
| ? TAcc | UnknownDeepKeyAndValue<TParent> | ||
| : unknown extends T // this stops runaway recursion when T is any | ||
| ? T | ||
| : T extends string | number | boolean | bigint | Date | ||
| ? TAcc | ||
| : T extends ReadonlyArray<any> | ||
| ? number extends T['length'] | ||
| ? DeepKeyAndValueArray<TParent, T, TAcc> | ||
| : DeepKeyAndValueTuple<TParent, T, TAcc> | ||
| : keyof T extends never | ||
| ? TAcc | UnknownDeepKeyAndValue<TParent> | ||
| : T extends object | ||
| ? DeepKeyAndValueObject<TParent, T, TAcc> | ||
| : TAcc | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular change addresses not just I see one test was adjusted to acknowledge this, but some additional LOC for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for review! |
||
|
|
||
| export type DeepRecord<T> = { | ||
| [TRecord in DeepKeysAndValues<T> as TRecord['key']]: TRecord['value'] | ||
|
|
@@ -211,3 +213,14 @@ export type FieldsMap<TFormData, TFieldGroupData> = | |
| TFieldGroupData[K] | ||
| > | ||
| } | ||
|
|
||
| type ExtractByNonNullableValue<T, TValue> = T extends { value: infer V } | ||
| ? [NonNullable<V>] extends [never] | ||
| ? never | ||
|
Comment on lines
+218
to
+219
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the previous behavior inferred never when the value was |
||
| : NonNullable<V> extends TValue | ||
| ? T | ||
| : never | ||
| : never | ||
|
|
||
| export type DeepKeysOfNonNullableType<TData, TValue> = | ||
| ExtractByNonNullableValue<DeepKeysAndValues<TData>, TValue>['key'] | ||
|
Comment on lines
+225
to
+226
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After changing When the value type is nullish, it previously evaluated as: However, after the change, it became: which now returns nullish. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect' | |
| import { useStore } from './useStore' | ||
| import type { | ||
| AnyFieldGroupApi, | ||
| DeepKeysOfType, | ||
| DeepKeysOfNonNullableType, | ||
| FieldGroupState, | ||
| FieldsMap, | ||
| FormAsyncValidateOrFn, | ||
|
|
@@ -35,7 +35,7 @@ export type AppFieldExtendedReactFieldGroupApi< | |
| TFormData, | ||
| TFieldGroupData, | ||
| TFields extends | ||
| | DeepKeysOfType<TFormData, TFieldGroupData | null | undefined> | ||
| | DeepKeysOfNonNullableType<TFormData, TFieldGroupData> | ||
| | FieldsMap<TFormData, TFieldGroupData>, | ||
| TOnMount extends undefined | FormValidateOrFn<TFormData>, | ||
| TOnChange extends undefined | FormValidateOrFn<TFormData>, | ||
|
|
@@ -91,7 +91,7 @@ export function useFieldGroup< | |
| TFormData, | ||
| TFieldGroupData, | ||
| TFields extends | ||
| | DeepKeysOfType<TFormData, TFieldGroupData | null | undefined> | ||
| | DeepKeysOfNonNullableType<TFormData, TFieldGroupData> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apply the same non-nullable field group fix to preact-form. |
||
| | FieldsMap<TFormData, TFieldGroupData>, | ||
| TOnMount extends undefined | FormValidateOrFn<TFormData>, | ||
| TOnChange extends undefined | FormValidateOrFn<TFormData>, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Measured with the following command.
Instantiationsincreased slightly on this branch's HEADc0d762ddcompared to base3886dcc5.packages/form-corepackages/react-formpackages/preact-formpackages/vue-formpackages/solid-formUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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-formLooks clean!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for checking — appreciate it.
Let me know if there’s anything else I should work on.