-
-
Notifications
You must be signed in to change notification settings - Fork 600
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
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 |
|---|---|---|
|
|
@@ -135,21 +135,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 | ||
|
|
||
| export type DeepRecord<T> = { | ||
| [TRecord in DeepKeysAndValues<T> as TRecord['key']]: TRecord['value'] | ||
|
|
@@ -194,3 +196,14 @@ export type FieldsMap<TFormData, TFieldGroupData> = | |
| TFieldGroupData[K] | ||
| > | ||
| } | ||
|
|
||
| type ExtractByNonNullableValue<T, TValue> = T extends { value: infer V } | ||
| ? [NonNullable<V>] extends [never] | ||
| ? never | ||
| : NonNullable<V> extends TValue | ||
| ? T | ||
| : never | ||
| : never | ||
|
|
||
| export type DeepKeysOfNonNullableType<TData, TValue> = | ||
| ExtractByNonNullableValue<DeepKeysAndValues<TData>, TValue>['key'] | ||
|
Comment on lines
+208
to
+209
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. |
||
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.
Since the previous behavior inferred never when the value was
nullish,the changes were updated to continue inferring never for
nullishvalue types.