@@ -6,9 +6,50 @@ import type { InternalFormInstance, NamePath, Store } from './interface';
6
6
import { useState , useContext , useEffect , useRef } from 'react' ;
7
7
import { getNamePath , getValue } from './utils/valueUtil' ;
8
8
9
- const useWatch = < ValueType = Store > ( dependencies : NamePath = [ ] , form ?: FormInstance ) => {
10
- const [ value , setValue ] = useState < ValueType > ( ) ;
11
- const valueCacheRef = useRef < ValueType > ( ) ;
9
+ type GetGeneric < Type > = Type extends FormInstance < infer ValueType > ? ValueType : never ;
10
+
11
+ function useWatch <
12
+ TDependencies1 extends keyof GetGeneric < TForm > ,
13
+ TForm extends FormInstance ,
14
+ TDependencies2 extends keyof GetGeneric < TForm > [ TDependencies1 ] ,
15
+ TDependencies3 extends keyof GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] ,
16
+ TDependencies4 extends keyof GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] ,
17
+ > (
18
+ dependencies : [ TDependencies1 , TDependencies2 , TDependencies3 , TDependencies4 ] ,
19
+ form ?: TForm ,
20
+ ) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] [ TDependencies4 ] ;
21
+
22
+ function useWatch <
23
+ TDependencies1 extends keyof GetGeneric < TForm > ,
24
+ TForm extends FormInstance ,
25
+ TDependencies2 extends keyof GetGeneric < TForm > [ TDependencies1 ] ,
26
+ TDependencies3 extends keyof GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] ,
27
+ > (
28
+ dependencies : [ TDependencies1 , TDependencies2 , TDependencies3 ] ,
29
+ form ?: TForm ,
30
+ ) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] ;
31
+
32
+ function useWatch <
33
+ TDependencies1 extends keyof GetGeneric < TForm > ,
34
+ TForm extends FormInstance ,
35
+ TDependencies2 extends keyof GetGeneric < TForm > [ TDependencies1 ] ,
36
+ > (
37
+ dependencies : [ TDependencies1 , TDependencies2 ] ,
38
+ form ?: TForm ,
39
+ ) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] ;
40
+
41
+ function useWatch < TDependencies extends keyof GetGeneric < TForm > , TForm extends FormInstance > (
42
+ dependencies : TDependencies | [ TDependencies ] ,
43
+ form ?: TForm ,
44
+ ) : GetGeneric < TForm > [ TDependencies ] ;
45
+
46
+ function useWatch < TForm extends FormInstance > ( dependencies : NamePath , form ?: TForm ) : any ;
47
+
48
+ function useWatch < ValueType = Store > ( dependencies : NamePath , form ?: FormInstance ) : ValueType ;
49
+
50
+ function useWatch ( dependencies : NamePath = [ ] , form ?: FormInstance ) {
51
+ const [ value , setValue ] = useState < any > ( ) ;
52
+ const valueCacheRef = useRef < any > ( ) ;
12
53
valueCacheRef . current = value ;
13
54
14
55
const fieldContext = useContext ( FieldContext ) ;
@@ -57,6 +98,6 @@ const useWatch = <ValueType = Store>(dependencies: NamePath = [], form?: FormIns
57
98
) ;
58
99
59
100
return value ;
60
- } ;
101
+ }
61
102
62
103
export default useWatch ;
0 commit comments