@tanstack/form-core 0.45.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/mergeForm.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { FormApi } from './FormApi'
2
- import type { NoInfer } from './util-types'
3
2
 
4
3
  function isValidKey(key: string | number | symbol): boolean {
5
4
  const dangerousProps = ['__proto__', 'constructor', 'prototype']
package/src/util-types.ts CHANGED
@@ -1,13 +1,7 @@
1
- type Nullable<T> = T | null
2
- type IsNullable<T> = [null] extends [T] ? true : false
3
-
4
- export type UnwrapOneLevelOfArray<T> = T extends (infer U)[] ? U : T
5
-
6
1
  /**
7
2
  * @private
8
3
  */
9
- export type RequiredByKey<T, K extends keyof T> = Omit<T, K> &
10
- Required<Pick<T, K>>
4
+ export type UnwrapOneLevelOfArray<T> = T extends (infer U)[] ? U : T
11
5
 
12
6
  type Narrowable = string | number | bigint | boolean
13
7
 
@@ -18,23 +12,12 @@ type NarrowRaw<A> =
18
12
  [K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]>
19
13
  }
20
14
 
21
- /**
22
- * @private
23
- */
24
- export type NoInfer<T> = [T][T extends any ? 0 : never]
25
-
26
- /**
27
- * @private
28
- */
29
- export type Narrow<A> = Try<A, [], NarrowRaw<A>>
30
-
31
15
  type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch
32
16
 
33
17
  /**
34
- * Hack to get TypeScript to show simplified types in error messages
35
18
  * @private
36
19
  */
37
- export type Pretty<T> = { [K in keyof T]: T[K] } & {}
20
+ export type Narrow<A> = Try<A, [], NarrowRaw<A>>
38
21
 
39
22
  type ComputeRange<
40
23
  N extends number,
@@ -105,6 +88,16 @@ type PrefixFromDepth<
105
88
  TDepth extends any[],
106
89
  > = TDepth['length'] extends 0 ? T : `.${T}`
107
90
 
91
+ // Hack changing Typescript's default get behavior in order to work with union objects
92
+ type Get<T, K extends string> = T extends { [Key in K]: infer V }
93
+ ? V
94
+ : T extends { [Key in K]?: infer W }
95
+ ? W | undefined
96
+ : never
97
+
98
+ type ApplyNull<T> = null extends T ? null : never
99
+ type ApplyUndefined<T> = undefined extends T ? undefined : never
100
+
108
101
  /**
109
102
  * Infer the type of a deeply nested property within an object or an array.
110
103
  */
@@ -113,36 +106,45 @@ export type DeepValue<
113
106
  TValue,
114
107
  // A string representing the path of the property we're trying to access
115
108
  TAccessor,
116
- TNullable extends boolean = IsNullable<TValue>,
117
- > =
118
- // If TValue is any it will recurse forever, this terminates the recursion
119
- unknown extends TValue
120
- ? TValue
109
+ // Depth for preventing infinite recursion
110
+ TDepth extends ReadonlyArray<any> = [],
111
+ > = unknown extends TValue // If TValue is any it will recurse forever, this terminates the recursion
112
+ ? TValue
113
+ : TDepth['length'] extends 10
114
+ ? unknown
121
115
  : // Check if we're looking for the property in an array
122
116
  TValue extends ReadonlyArray<any>
123
117
  ? TAccessor extends `[${infer TBrackets}].${infer TAfter}`
124
118
  ? /*
125
119
  Extract the first element from the accessor path (`TBrackets`)
126
120
  and recursively call `DeepValue` with it
127
- */
128
- DeepValue<DeepValue<TValue, TBrackets>, TAfter>
121
+ */
122
+ DeepValue<
123
+ DeepValue<TValue, TBrackets, [...TDepth, any]>,
124
+ TAfter,
125
+ [...TDepth, any]
126
+ >
129
127
  : TAccessor extends `[${infer TBrackets}]`
130
- ? DeepValue<TValue, TBrackets>
128
+ ? DeepValue<TValue, TBrackets, [...TDepth, any]>
131
129
  : TAccessor extends keyof TValue
132
130
  ? TValue[TAccessor]
133
131
  : TValue[TAccessor & number]
134
- : // Check if we're looking for the property in an object
135
- TValue extends Record<string | number, any>
136
- ? TAccessor extends `${infer TBefore}[${infer TEverythingElse}`
137
- ? DeepValue<DeepValue<TValue, TBefore>, `[${TEverythingElse}`>
138
- : TAccessor extends `[${infer TBrackets}]`
139
- ? DeepValue<TValue, TBrackets>
140
- : TAccessor extends `${infer TBefore}.${infer TAfter}`
141
- ? DeepValue<DeepValue<TValue, TBefore>, TAfter>
142
- : TAccessor extends string
143
- ? TNullable extends true
144
- ? Nullable<TValue[TAccessor]>
145
- : TValue[TAccessor]
146
- : never
147
- : // Do not allow `TValue` to be anything else
148
- never
132
+ : TAccessor extends `${infer TBefore}[${infer TEverythingElse}`
133
+ ? DeepValue<
134
+ DeepValue<TValue, TBefore, [...TDepth, any]>,
135
+ `[${TEverythingElse}`,
136
+ [...TDepth, any]
137
+ >
138
+ : TAccessor extends `[${infer TBrackets}]`
139
+ ? DeepValue<TValue, TBrackets, [...TDepth, any]>
140
+ : TAccessor extends `${infer TBefore}.${infer TAfter}`
141
+ ? DeepValue<
142
+ DeepValue<TValue, TBefore, [...TDepth, any]>,
143
+ TAfter,
144
+ [...TDepth, any]
145
+ >
146
+ : TAccessor extends string
147
+ ?
148
+ | Get<TValue, TAccessor>
149
+ | (ApplyNull<TValue> | ApplyUndefined<TValue>)
150
+ : never
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,31 +0,0 @@
1
- import { assertType, it } from 'vitest'
2
- import { FormApi } from '../src/index'
3
-
4
- it('should type handleSubmit as never when onSubmitMeta is not passed', () => {
5
- const form = new FormApi({
6
- defaultValues: {
7
- name: 'test',
8
- },
9
- } as const)
10
-
11
- assertType<() => Promise<void>>(form.handleSubmit)
12
- })
13
-
14
- type OnSubmitMeta = {
15
- group: string
16
- }
17
-
18
- it('should type handleChange correctly', () => {
19
- const form = new FormApi({
20
- defaultValues: {
21
- name: 'test',
22
- },
23
- onSubmitMeta: {} as OnSubmitMeta,
24
- } as const)
25
-
26
- form.handleSubmit({ group: 'track' })
27
-
28
- assertType<(submitMeta: { group: string }) => Promise<void>>(
29
- form.handleSubmit,
30
- )
31
- })