Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit fa3468f

Browse files
committed
Fix build function type
1 parent 973c6fe commit fa3468f

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/types/formHandler.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { ComputedRef, Ref } from '@vue/runtime-core'
22
import { RegisterOptions, RegisterReturn } from './register'
33

4+
type ComputableUnion<T> = T | Ref<T> | ComputedRef<T>
5+
6+
type ConfigType<T> = Partial<Record<keyof T, RegisterOptions>>
7+
8+
type PossiblePromise<T> = T | Promise<T>
9+
410
export interface FormState<T> {
511
isDirty: boolean
612
isTouched: boolean
@@ -17,11 +23,15 @@ export type HandleSubmitErrorFn<T> = (errors: FormState<T>['errors']) => void
1723
export type HandleSubmitSuccessFn<T> = (values: Record<keyof T, any>) => void
1824

1925
export type Build<T extends Record<string, any> = Record<string, any>> = <
20-
TBuild extends Partial<Record<keyof T, RegisterOptions>>,
26+
TBuild extends Partial<T>,
2127
>(
22-
configuration: TBuild | Ref<TBuild> | ComputedRef<TBuild>
23-
) => ComputedRef<Record<keyof TBuild, Readonly<RegisterReturn<TBuild>>>>
24-
28+
configuration: ComputableUnion<ConfigType<TBuild>>
29+
) => ComputedRef<
30+
Record<
31+
keyof TBuild,
32+
Readonly<RegisterReturn<Partial<Record<keyof TBuild, RegisterOptions>>>>
33+
>
34+
>
2535
export interface PartialReturn<T> {
2636
/** Current form values */
2737
values: Partial<T>
@@ -79,23 +89,23 @@ export interface FormHandlerReturn<T extends Record<string, any>>
7989

8090
export type Interceptor<T> = (
8191
_: InterceptorParams<T>
82-
) => Promise<boolean> | boolean
92+
) => PossiblePromise<boolean>
8393

8494
export type SubmitValidation = (
8595
values: Record<string, any>
86-
) => Promise<boolean> | boolean
96+
) => PossiblePromise<boolean>
8797

8898
export type InjectionKey = string | Symbol
8999

90100
export interface FormHandlerParams<TForm, TInitial> {
91101
/** Values to initialize the form */
92-
initialValues?: TInitial | Ref<TInitial> | ComputedRef<TInitial>
102+
initialValues?: ComputableUnion<TInitial>
93103

94104
/** Field change interceptor */
95105
interceptor?: Interceptor<TForm>
96106

97107
/** Validation function to execute before submitting (when using this individual validations are invalidated) */
98-
validate?: (values: Partial<TForm>) => Promise<boolean> | boolean
108+
validate?: (values: Partial<TForm>) => PossiblePromise<boolean>
99109

100110
/** Validation behavior options */
101111
validationMode?: 'onChange' | 'onBlur' | 'onSubmit' | 'always'

0 commit comments

Comments
 (0)