1
1
import { Validators } from '@angular/forms' ;
2
2
import { expectTypeOf } from 'expect-type' ;
3
3
import { Observable , of , Subject , Subscription } from 'rxjs' ;
4
- import { FormControl , FormGroup } from '..' ;
4
+ import { ControlsOf , FormControl , FormGroup , ValuesOf } from '..' ;
5
5
import { ControlState } from './core' ;
6
6
import { FormArray } from './form-array' ;
7
7
8
8
type Base = {
9
9
name : string ;
10
+ nameControl : FormControl < string > ;
10
11
phone : {
11
12
prefix : string ;
12
13
number : string ;
13
14
} ;
15
+ phoneControl : FormControl < {
16
+ prefix : string ;
17
+ number : string ;
18
+ } > ;
19
+ phoneGroup : FormGroup < ControlsOf < {
20
+ prefix : string ;
21
+ number : string ;
22
+ } > >
23
+ deep : Base [ ] ;
24
+ deepFormArray : FormArray < Base > ;
14
25
} ;
15
26
27
+ type ValueOfBase = {
28
+ name : string ;
29
+ nameControl : string ;
30
+ phone : {
31
+ prefix : string ;
32
+ number : string ;
33
+ } ;
34
+ phoneControl :{
35
+ prefix : string ;
36
+ number : string ;
37
+ } ;
38
+ phoneGroup :{
39
+ prefix : string ;
40
+ number : string ;
41
+ } ;
42
+ deep : ValueOfBase [ ] ;
43
+ deepFormArray : ValueOfBase [ ] ;
44
+ }
45
+
16
46
describe ( 'FormArray Types' , ( ) => {
17
47
it ( 'should infer primitives' , ( ) => {
18
48
const arr = new FormArray < string > ( [ new FormControl ( '' ) ] ) ;
@@ -56,26 +86,48 @@ describe('FormArray Types', () => {
56
86
const arr = new FormArray < Base > ( [
57
87
new FormGroup ( {
58
88
name : new FormControl ( '' ) ,
89
+ nameControl : new FormControl ( '' ) ,
59
90
phone : new FormGroup ( {
60
91
prefix : new FormControl ( '' ) ,
61
92
number : new FormControl ( '' ) ,
62
93
} ) ,
94
+ phoneControl : new FormControl ( {
95
+ prefix : '' ,
96
+ number : '' ,
97
+ } ) ,
98
+ phoneGroup : new FormGroup ( {
99
+ prefix : new FormControl ( '' ) ,
100
+ number : new FormControl ( '' ) ,
101
+ } ) ,
102
+ deep : new FormArray ( [ ] ) ,
103
+ deepFormArray : new FormArray ( [ ] ) ,
63
104
} ) ,
64
105
] ) ;
65
106
66
- expectTypeOf ( arr . value ) . toEqualTypeOf < Base [ ] > ( ) ;
67
- expectTypeOf ( arr . valueChanges ) . toEqualTypeOf < Observable < Base [ ] > > ( ) ;
68
- expectTypeOf ( arr . value$ ) . toEqualTypeOf < Observable < Base [ ] > > ( ) ;
107
+ expectTypeOf ( arr . value ) . toEqualTypeOf < ValueOfBase [ ] > ( ) ;
108
+ expectTypeOf ( arr . valueChanges ) . toEqualTypeOf < Observable < ValueOfBase [ ] > > ( ) ;
109
+ expectTypeOf ( arr . value$ ) . toEqualTypeOf < Observable < ValueOfBase [ ] > > ( ) ;
69
110
70
111
// @ts -expect-error - should be typed
71
112
arr . insert ( 0 , new FormControl ( 1 ) ) ;
72
113
arr . push (
73
114
new FormGroup ( {
74
115
name : new FormControl ( '' ) ,
116
+ nameControl : new FormControl ( '' ) ,
75
117
phone : new FormGroup ( {
76
118
prefix : new FormControl ( '' ) ,
77
119
number : new FormControl ( '' ) ,
78
120
} ) ,
121
+ phoneControl : new FormControl ( {
122
+ prefix : '' ,
123
+ number : '' ,
124
+ } ) ,
125
+ phoneGroup : new FormGroup ( {
126
+ prefix : new FormControl ( '' ) ,
127
+ number : new FormControl ( '' ) ,
128
+ } ) ,
129
+ deep : new FormArray ( [ ] ) ,
130
+ deepFormArray : new FormArray ( [ ] ) ,
79
131
} )
80
132
) ;
81
133
@@ -90,7 +142,7 @@ describe('FormArray Types', () => {
90
142
) ;
91
143
92
144
const first$ = arr . select ( ( state ) => {
93
- expectTypeOf ( state ) . toEqualTypeOf < Base [ ] > ( ) ;
145
+ expectTypeOf ( state ) . toEqualTypeOf < ValueOfBase [ ] > ( ) ;
94
146
95
147
return state [ 0 ] ;
96
148
} ) ;
@@ -100,19 +152,19 @@ describe('FormArray Types', () => {
100
152
// @ts -expect-error - should be typed
101
153
arr . reset ( { foo : '' } ) ;
102
154
103
- arr . reset ( [ { name : '' , phone : { prefix : '' , number : '' } } ] ) ;
155
+ arr . reset ( [ { name : '' , phone : { prefix : '' , number : '' } , nameControl : '' , phoneControl : { prefix : '' , number : '' } , phoneGroup : { prefix : '' , number : '' } , deep : [ ] , deepFormArray : [ ] } ] ) ;
104
156
105
- expectTypeOf ( arr . getRawValue ( ) ) . toEqualTypeOf < Base [ ] > ( ) ;
157
+ expectTypeOf ( arr . getRawValue ( ) ) . toEqualTypeOf < ValueOfBase [ ] > ( ) ;
106
158
} ) ;
107
159
108
160
it ( 'should infer setValue' , ( ) => {
109
161
const arr = new FormArray < Base > ( [ ] ) ;
110
162
111
163
try {
112
- arr . setValue ( [ { name : '' , phone : { number : '' , prefix : '' } } ] ) ;
164
+ arr . setValue ( [ { name : '' , phone : { prefix : '' , number : '' } , nameControl : '' , phoneControl : { prefix : '' , number : '' } , phoneGroup : { prefix : '' , number : '' } , deep : [ ] , deepFormArray : [ ] } ] ) ;
113
165
114
166
const sub = arr . setValue (
115
- of ( [ { name : '' , phone : { number : '' , prefix : '' } } ] )
167
+ of ( [ { name : '' , phone : { prefix : '' , number : '' } , nameControl : '' , phoneControl : { prefix : '' , number : '' } , phoneGroup : { prefix : '' , number : '' } , deep : [ ] , deepFormArray : [ ] } ] )
116
168
) ;
117
169
expectTypeOf ( sub ) . toEqualTypeOf < Subscription > ( ) ;
118
170
} catch {
@@ -211,7 +263,7 @@ describe('FormArray Functionality', () => {
211
263
} ) ;
212
264
213
265
it ( 'should invalidChanges$' , ( ) => {
214
- const control = new FormArray ( [ new FormControl ( null , Validators . required ) ] ) ;
266
+ const control = new FormArray ( [ new FormControl < string | null > ( null , Validators . required ) ] ) ;
215
267
const spy = jest . fn ( ) ;
216
268
control . invalid$ . subscribe ( spy ) ;
217
269
expect ( spy ) . toHaveBeenCalledWith ( true ) ;
@@ -222,7 +274,7 @@ describe('FormArray Functionality', () => {
222
274
} ) ;
223
275
224
276
it ( 'should validChanges$' , ( ) => {
225
- const control = new FormArray ( [ new FormControl ( null , Validators . required ) ] ) ;
277
+ const control = new FormArray ( [ new FormControl < string | null > ( null , Validators . required ) ] ) ;
226
278
const spy = jest . fn ( ) ;
227
279
control . valid$ . subscribe ( spy ) ;
228
280
expect ( spy ) . toHaveBeenCalledWith ( false ) ;
0 commit comments