admin管理员组文章数量:1405195
I'm working on a reusable FormArray
component and am trying to make better use of Typescript's generics features to minimize my code. I created an interface for FormArray
s that iterate FormControl
s by doing this
export interface FormArrayControlGroup<T>{
list : FormArray< FormControl< T | null >;
}
Then I apply it to a prop like this
export interface SomeFormGroup{
someProp : FormGroup< FormArrayControlGroup< myCustomType > >;
}
This works wonderfully so I went to create one for FormArray
s that iterate FormGroups
by doing this
export interface FormArrayGROUPgroup< T >{
list : FormArray< FormGroup< T > >;
}
However that causes the following error
Type 'T' does not satisfy the constraint '{ [K in keyof T]: AbstractControl<any, any>; }
Plus if I'm not mistaking FormGroup
is already of type <T>
which is what allows us to infer our types into it. What do I need to do to get it to work the way I'm trying to use it? I'm forcing myself to dig more into generics in typescript because I've just never been able to identify a need to go that deep into it, so I have no clue what to even search for.
I'm working on a reusable FormArray
component and am trying to make better use of Typescript's generics features to minimize my code. I created an interface for FormArray
s that iterate FormControl
s by doing this
export interface FormArrayControlGroup<T>{
list : FormArray< FormControl< T | null >;
}
Then I apply it to a prop like this
export interface SomeFormGroup{
someProp : FormGroup< FormArrayControlGroup< myCustomType > >;
}
This works wonderfully so I went to create one for FormArray
s that iterate FormGroups
by doing this
export interface FormArrayGROUPgroup< T >{
list : FormArray< FormGroup< T > >;
}
However that causes the following error
Type 'T' does not satisfy the constraint '{ [K in keyof T]: AbstractControl<any, any>; }
Plus if I'm not mistaking FormGroup
is already of type <T>
which is what allows us to infer our types into it. What do I need to do to get it to work the way I'm trying to use it? I'm forcing myself to dig more into generics in typescript because I've just never been able to identify a need to go that deep into it, so I have no clue what to even search for.
1 Answer
Reset to default 2The official FormGroup
declaration requires specific generic type:
export declare class FormGroup<TControl extends {
[K in keyof TControl]: AbstractControl<any>;
} = any>
Watch out with TControl
generic parameter must be an object where each key is associated with an AbstractControl.
If we pass a generic type T
without ensuring it follows this structure, will infer it as =any
.
Example: stackblitz example or demo repo github
So to resolve your case would be in my demo I use items instead of list
:
export interface FormArrayGROUPgroup<T extends { [K in keyof T]: AbstractControl<any, any> }> {
list: FormArray<FormGroup<T>>;
}
本文标签: angularHow can we infer a generic type through an interface onto a FormGroupStack Overflow
版权声明:本文标题:angular - How can we infer a generic type through an interface onto a FormGroup? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744876115a2629924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论