File tree 2 files changed +17
-7
lines changed 2 files changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -3,10 +3,14 @@ This function takes an initial value and a yup schema.
3
3
This function returns ` Field ` object.
4
4
5
5
## Parameters
6
- The ` field() ` takes 3 parameters:
6
+ The ` field() ` takes 3 parameters and 2 generic type parameters :
7
7
8
8
``` typescript
9
- function field<T >(value : T | Ref <T >, schema ? : FieldSchema , validateOptions ? : ValidateOptions ): Field <T >
9
+ function field<T , U extends T = T >(
10
+ value : T | Ref <T >,
11
+ schema ? : FieldSchema ,
12
+ validateOptions ? : ValidateOptions
13
+ ): Field <T , T extends U ? T : U >;
10
14
```
11
15
12
16
### 1. ` value ` <Badge type =" danger " text =" Required " />
@@ -67,6 +71,12 @@ Since `validateSync()` is used internally, Async tests are not available.
67
71
### 3. ` validateOptions ` <Badge type =" info " text =" Optional " />
68
72
- Pass ` { abortEarly: false } ` if all validation errors by the yup schema are required.
69
73
74
+ ### ` T ` <Badge type =" info " text =" Optional " />
75
+ - Pass the type of ` $value ` explicitly like ` ref<T>() ` .
76
+
77
+ ### ` U ` <Badge type =" info " text =" Optional / Default: T " />
78
+ - Pass the preferred type as the result of [ ` toObject() ` ] ( /api/toObject ) .
79
+
70
80
## Details of ` Field ` object
71
81
### ` $value ` property
72
82
- The field value that can be set to ` v-model ` .
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ export type FormPropType<T extends () => Form> = PropType<
32
32
ReturnType < T > & { $key ?: number }
33
33
> ;
34
34
35
- class Field < T > {
35
+ class Field < T , U extends T = T > {
36
36
private readonly $_valueRef : Ref < T > ;
37
37
private readonly $_errorRef : ComputedRef < yup . ValidationError | undefined > ;
38
38
public readonly $label : string ;
@@ -210,11 +210,11 @@ export function defineForm<T extends Form>(form: T): T {
210
210
return form ;
211
211
}
212
212
213
- export function field < T > (
213
+ export function field < T , U extends T = T > (
214
214
value : T | Ref < T > ,
215
215
schema ?: FieldSchema ,
216
216
validateOptions ?: ValidateOptions
217
- ) : Field < T > {
217
+ ) : Field < T , T extends U ? T : U > {
218
218
return new Field ( value , schema , validateOptions ) ;
219
219
}
220
220
@@ -268,8 +268,8 @@ type ToObjectOutput<T extends Form> = {
268
268
? never
269
269
: Exclude < K , "$key" > ] : T [ K ] extends Form
270
270
? ToObjectOutput < T [ K ] >
271
- : T [ K ] extends Field < any >
272
- ? T [ K ] [ "$value" ]
271
+ : T [ K ] extends Field < any , infer U >
272
+ ? U
273
273
: T [ K ] extends FormsField < ( arg : any ) => Form >
274
274
? ToObjectOutput < T [ K ] [ "$forms" ] [ number ] > [ ]
275
275
: never ;
You can’t perform that action at this time.
0 commit comments