1- import  {  NamedInputEvent ,  RequestMethod ,   SimpleValidationErrors ,  toSimpleValidationErrors ,  ValidationConfig ,  ValidationErrors ,  resolveUrl ,  resolveMethod  }  from  'laravel-precognition' 
1+ import  {  NamedInputEvent ,  SimpleValidationErrors ,  toSimpleValidationErrors ,  ValidationConfig ,  ValidationErrors ,  resolveUrl ,  resolveMethod  }  from  'laravel-precognition' 
22import  {  useForm  as  usePrecognitiveForm ,  client  }  from  'laravel-precognition-vue' 
33import  {  useForm  as  useInertiaForm  }  from  '@inertiajs/vue3' 
4- import  {  VisitOptions  }  from  '@inertiajs/core' 
4+ import  {  VisitOptions ,   Method ,   FormDataKeys ,   FormDataType ,   type   FormDataErrors ,   type   ErrorValue  }  from  '@inertiajs/core' 
55import  {  watchEffect  }  from  'vue' 
6- import  {  Form ,  FormDataConvertible  }  from  './types' 
6+ import  type   {  Form ,  FormDataConvertible  }  from  './types' 
77
8- export  {  client ,  Form  } 
8+ export  {  client  } 
9+ export  type  {  Form  } 
10+ export  {  Form  as  PrecognitionForm  }  from  "./form" ; 
911
10- export  const  useForm  =  < Data  extends  Record < string ,   FormDataConvertible > > ( method : RequestMethod  |  ( ( )  =>  RequestMethod ) ,  url : string  |  ( ( )  =>  string ) ,  inputs : Data  |  ( ( )  =>  Data ) ,  config : ValidationConfig  =  { } ) : Form < Data >  =>  { 
12+ export  const  useForm  =  < Data  extends  FormDataType < Data > > ( method : Method  |  ( ( )  =>  Method ) ,  url : string  |  ( ( )  =>  string ) ,  inputs : Data  |  ( ( )  =>  Data ) ,  config : ValidationConfig  =  { } ) : Form < Data >  =>  { 
1113    /** 
1214     * The Inertia form. 
1315     */ 
@@ -66,7 +68,7 @@ export const useForm = <Data extends Record<string, FormDataConvertible>>(method
6668    const  form : Form < Data >  =  Object . assign ( inertiaForm ,  { 
6769        validating : precognitiveForm . validating , 
6870        touched : precognitiveForm . touched , 
69-         touch ( name : Array < string >  |  string  |  NamedInputEvent )  { 
71+         touch ( name : Array < FormDataKeys < Data > >  |  FormDataKeys < Data >  |  NamedInputEvent )  { 
7072            precognitiveForm . touch ( name ) 
7173
7274            return  form 
@@ -81,34 +83,63 @@ export const useForm = <Data extends Record<string, FormDataConvertible>>(method
8183
8284            return  form 
8385        } , 
84-         clearErrors ( ...names : string [ ] )  { 
86+         clearErrors < K   extends   FormDataKeys < Data > > ( ...names : K [ ] )  { 
8587            inertiaClearErrors ( ...names ) 
8688
8789            if  ( names . length  ===  0 )  { 
8890                precognitiveForm . setErrors ( { } ) 
8991            }  else  { 
90-                 names . forEach ( precognitiveForm . forgetError ) 
92+                 names . forEach ( ( n )  => 
93+                     precognitiveForm . forgetError ( 
94+                         String ( n )  as  unknown  as  keyof  Data , 
95+                     ) , 
96+                 ) 
9197            } 
9298
9399            return  form 
94100        } , 
95-         reset ( ...names : string [ ] )  { 
101+         reset < K   extends   FormDataKeys < Data > > ( ...names : K [ ] )  { 
96102            inertiaReset ( ...names ) 
97103
98-             precognitiveForm . reset ( ...names ) 
104+             if  ( names . length  ===  0 )  { 
105+                 precognitiveForm . reset ( ) ; 
106+             }  else  { 
107+                 const  str  =  names . map ( ( n )  =>  String ( n ) ) ; 
108+                 precognitiveForm . reset ( 
109+                     ...( str  as  unknown  as  Array < keyof  Data > ) , 
110+                 ) ; 
111+             } 
112+ 
113+             return  form ; 
99114        } , 
100115        setErrors ( errors : SimpleValidationErrors  |  ValidationErrors )  { 
101-             // @ts -expect-error 
102-             precognitiveForm . setErrors ( errors ) 
116+             const  anyErr  =  errors  as  unknown  as  Record < string ,  unknown > 
117+             const  firstVal  =  anyErr  ? Object . values ( anyErr ) [ 0 ]  : undefined 
118+             const  simple : SimpleValidationErrors  = 
119+                 typeof  firstVal  ===  "string" 
120+                     ? ( errors  as  SimpleValidationErrors ) 
121+                     : toSimpleValidationErrors ( errors  as  ValidationErrors ) 
122+ 
123+             precognitiveForm . setErrors ( 
124+                 simple  as  unknown  as  Partial < 
125+                     Record < keyof  Data ,  string  |  string [ ] > 
126+                 > , 
127+             ) 
103128
104129            return  form 
105130        } , 
106-         forgetError ( name : string  |  NamedInputEvent )  { 
107-             precognitiveForm . forgetError ( name ) 
131+         forgetError ( name : FormDataKeys < Data >  |  NamedInputEvent )  { 
132+             if  ( typeof  name  ===  "object"  &&  "target"  in  name )  { 
133+                 precognitiveForm . forgetError ( name ) 
134+             }  else  { 
135+                 precognitiveForm . forgetError ( 
136+                     String ( name )  as  unknown  as  keyof  Data , 
137+                 ) 
138+             } 
108139
109140            return  form 
110141        } , 
111-         setError ( key : ( keyof   Data )  |  Record < keyof   Data ,   string > ,  value ?: string )  { 
142+         setError ( key : FormDataKeys < Data >  |  FormDataErrors < Data > ,  value ?: ErrorValue )  { 
112143            let  errors : SimpleValidationErrors 
113144
114145            if  ( typeof  key  !==  'object' )  { 
@@ -135,7 +166,7 @@ export const useForm = <Data extends Record<string, FormDataConvertible>>(method
135166
136167            return  form 
137168        } , 
138-         validate ( name ?: string  |  NamedInputEvent  |  ValidationConfig ,  config ?: ValidationConfig )  { 
169+         validate ( name ?: FormDataKeys < Data >  |  NamedInputEvent  |  ValidationConfig ,  config ?: ValidationConfig )  { 
139170            precognitiveForm . setData ( transformer ( inertiaForm . data ( ) ) ) 
140171
141172            if  ( typeof  name  ===  'object'  &&  ! ( 'target'  in  name ) )  { 
@@ -150,8 +181,13 @@ export const useForm = <Data extends Record<string, FormDataConvertible>>(method
150181
151182            if  ( typeof  name  ===  'undefined' )  { 
152183                precognitiveForm . validate ( config ) 
153-             }  else  { 
184+             }  else  if   ( typeof   name   ===   "object"   &&   "target"   in   name )   { 
154185                precognitiveForm . validate ( name ,  config ) 
186+             }  else  { 
187+                 precognitiveForm . validate ( 
188+                     String ( name )  as  unknown  as  keyof  Data , 
189+                     config , 
190+                 ) 
155191            } 
156192
157193            return  form 
@@ -166,7 +202,7 @@ export const useForm = <Data extends Record<string, FormDataConvertible>>(method
166202
167203            return  form 
168204        } , 
169-         submit ( submitMethod : RequestMethod  |  Partial < VisitOptions >  =  { } ,  submitUrl ?: string ,  submitOptions ?: Partial < VisitOptions > ) : void { 
205+         submit ( submitMethod : Method  |  Partial < VisitOptions >  =  { } ,  submitUrl ?: string ,  submitOptions ?: Partial < VisitOptions > ) : void { 
170206            if  ( typeof  submitMethod  !==  'string' )  { 
171207                submitOptions  =  submitMethod 
172208                submitUrl  =  resolveUrl ( url ) 
0 commit comments