@@ -14,8 +14,20 @@ type ValidateAsyncFn<TData, TFormData> = (
14
14
fieldApi : FieldApi < TData , TFormData > ,
15
15
) => ValidationError | Promise < ValidationError >
16
16
17
- export interface FieldOptions < TData , TFormData > {
18
- name : unknown extends TFormData ? string : DeepKeys < TFormData >
17
+ export interface FieldOptions <
18
+ _TData ,
19
+ TFormData ,
20
+ /**
21
+ * This allows us to restrict the name to only be a valid field name while
22
+ * also assigning it to a generic
23
+ */
24
+ TName = unknown extends TFormData ? string : DeepKeys < TFormData > ,
25
+ /**
26
+ * If TData is unknown, we can use the TName generic to determine the type
27
+ */
28
+ TData = unknown extends _TData ? DeepValue < TFormData , TName > : _TData ,
29
+ > {
30
+ name : TName
19
31
index ?: TData extends any [ ] ? number : never
20
32
defaultValue ?: TData
21
33
asyncDebounceMs ?: number
@@ -75,7 +87,7 @@ export type FieldState<TData> = {
75
87
}
76
88
77
89
/**
78
- * TData may not known at the time of FieldApi construction, so we need to
90
+ * TData may not be known at the time of FieldApi construction, so we need to
79
91
* use a conditional type to determine if TData is known or not.
80
92
*
81
93
* If TData is not known, we use the TFormData type to determine the type of
@@ -89,7 +101,13 @@ export class FieldApi<TData, TFormData> {
89
101
uid : number
90
102
form : FormApi < TFormData >
91
103
name ! : DeepKeys < TFormData >
92
- // This is a hack that allows us to use `GetTData` without calling it everywhere
104
+ /**
105
+ * This is a hack that allows us to use `GetTData` without calling it everywhere
106
+ *
107
+ * Unfortunately this hack appears to be needed alongside the `TName` hack
108
+ * further up in this file. This properly types all of the internal methods,
109
+ * while the `TName` hack types the options properly
110
+ */
93
111
_tdata ! : GetTData < typeof this . name , TData , TFormData >
94
112
store ! : Store < FieldState < typeof this . _tdata > >
95
113
state ! : FieldState < typeof this . _tdata >
@@ -253,7 +271,7 @@ export class FieldApi<TData, TFormData> {
253
271
// track freshness of the validation
254
272
const validationCount = ( this . getInfo ( ) . validationCount || 0 ) + 1
255
273
this . getInfo ( ) . validationCount = validationCount
256
- const error = normalizeError ( validate ( value , this as never ) )
274
+ const error = normalizeError ( validate ( value as never , this as never ) )
257
275
258
276
if ( this . state . meta . error !== error ) {
259
277
this . setMeta ( ( prev ) => ( {
@@ -336,7 +354,7 @@ export class FieldApi<TData, TFormData> {
336
354
// Only kick off validation if this validation is the latest attempt
337
355
if ( checkLatest ( ) ) {
338
356
try {
339
- const rawError = await validate ( value , this as never )
357
+ const rawError = await validate ( value as never , this as never )
340
358
341
359
if ( checkLatest ( ) ) {
342
360
const error = normalizeError ( rawError )
0 commit comments