66 FormControl ,
77 FormArray ,
88 AbstractControl ,
9+ FormGroup ,
910} from '@angular/forms' ;
1011import { InjectionToken , Type , forwardRef , OnDestroy } from '@angular/core' ;
1112import { Observable , Subject , timer } from 'rxjs' ;
@@ -19,7 +20,10 @@ export type ControlsNames<T> = { [K in keyof T]-?: K };
1920
2021export type ControlMap < T , V > = { [ K in keyof T ] -?: V } ;
2122
22- export type ControlsType < T > = { [ K in keyof T ] -?: T [ K ] extends any [ ] ? FormArray : AbstractControl } ;
23+ export type ControlsType < T > = {
24+ [ K in keyof T ] -?: T [ K ] extends any [ ] ? TypedFormArray < T [ K ] > : TypedFormControl < T [ K ] > | TypedFormGroup < T [ K ] > ;
25+ } ;
26+
2327export type FormErrorsType < T > = {
2428 [ K in keyof T ] -?: T [ K ] extends any [ ] ? ( null | ValidationErrors ) [ ] : ValidationErrors ;
2529} ;
@@ -32,6 +36,37 @@ export type FormErrors<FormInterface> = null | Partial<
3236 }
3337> ;
3438
39+ // using set/patch value options signature from form controls to allow typing without additional casting
40+ export interface TypedAbstractControl < TValue > extends AbstractControl {
41+ value : TValue ;
42+ valueChanges : Observable < TValue > ;
43+ setValue ( value : TValue , options ?: Parameters < AbstractControl [ 'setValue' ] > [ 1 ] ) : void ;
44+ patchValue ( value : Partial < TValue > , options ?: Parameters < AbstractControl [ 'patchValue' ] > [ 1 ] ) : void ;
45+ }
46+
47+ export interface TypedFormGroup < TValue > extends FormGroup {
48+ value : TValue ;
49+ valueChanges : Observable < TValue > ;
50+ controls : ControlsType < TValue > ;
51+ setValue ( value : TValue , options ?: Parameters < FormGroup [ 'setValue' ] > [ 1 ] ) : void ;
52+ patchValue ( value : Partial < TValue > , options ?: Parameters < FormGroup [ 'patchValue' ] > [ 1 ] ) : void ;
53+ }
54+
55+ export interface TypedFormArray < TValue extends any [ ] > extends FormArray {
56+ value : TValue ;
57+ valueChanges : Observable < TValue > ;
58+ controls : TypedAbstractControl < TValue > [ ] ;
59+ setValue ( value : TValue , options ?: Parameters < FormArray [ 'setValue' ] > [ 1 ] ) : void ;
60+ patchValue ( value : TValue , options ?: Parameters < FormArray [ 'patchValue' ] > [ 1 ] ) : void ;
61+ }
62+
63+ export interface TypedFormControl < TValue > extends FormGroup {
64+ value : TValue ;
65+ valueChanges : Observable < TValue > ;
66+ setValue ( value : TValue , options ?: Parameters < FormControl [ 'setValue' ] > [ 1 ] ) : void ;
67+ patchValue ( value : Partial < TValue > , options ?: Parameters < FormControl [ 'patchValue' ] > [ 1 ] ) : void ;
68+ }
69+
3570export type KeysWithType < T , V > = { [ K in keyof T ] : T [ K ] extends V ? K : never } [ keyof T ] ;
3671
3772export type ArrayPropertyKey < T > = KeysWithType < T , Array < any > > ;
0 commit comments