Angular (15) 无法扩展类型化的 FormGroup

huangapple go评论64阅读模式
英文:

Angular (15) Unable to extend a typed FormGroup

问题

我在使用类型化的 FormGroup 进行实验。

我创建了以下内容:

export interface CustomFormControls {
  name: FormControl<string | null>;
  content: FormControl<string | null>;
}

export class CustomForm extends FormGroup<CustomFormControls> {
  constructor() {
    super({
      name: new FormControl('name'),
      content: new FormControl('content'),
    });
  }
}

然后我尝试在一个组件中使用它:

form: CustomForm = new CustomForm();

以及在模板中:

<form class="Form" [formGroup]="form">

然而,我遇到了以下错误:

Type 'CustomForm' is not assignable to type 'FormGroup<any>'.
  Types of property 'controls' are incompatible.
    Type 'CustomFormControls' is not assignable to type '{ [key: string]: AbstractControl<any, any>; }'.
      Index signature for type 'string' is missing in type 'CustomFormControls'.
     <form class="Form" [formGroup]="form">

我不确定我在这里做错了什么。

英文:

I am messing around with typed FormGroup.

I created the following:

export interface CustomFormControls {
  name: FormControl&lt;string|null&gt;
  content: FormControl&lt;string|null&gt;
}

export class CustomForm extends FormGroup&lt;CustomFormControls&gt; {
    constructor() {
        super({
            name: new FormControl(&#39;name&#39;),
            content: new FormControl(&#39;content&#39;),
        })
    }
}

and then I am trying to use it in a component:

form: CustomForm = new CustomForm()

and in my template:

&lt;form class=&quot;Form&quot; [formGroup]=&quot;form&quot;&gt;

However, I am getting the following error:

Type &#39;CustomForm&#39; is not assignable to type &#39;FormGroup&lt;any&gt;&#39;.
  Types of property &#39;controls&#39; are incompatible.
    Type &#39;CustomFormControls&#39; is not assignable to type &#39;{ [key: string]: AbstractControl&lt;any, any&gt;; }&#39;.
      Index signature for type &#39;string&#39; is missing in type &#39;CustomFormControls&#39;.
         &lt;form class=&quot;Form&quot; [formGroup]=&quot;form&quot;&gt;

I am not sure to understand what I did wrong in there.

答案1

得分: 4

export interface CustomFormControls {
name: FormControl<string|null>;
content: FormControl<string|null>;
[key: string]: AbstractControl<any, any>;
}

英文:

Try adding an index signature for a string key

export interface CustomFormControls {
  name: FormControl&lt;string|null&gt;
  content: FormControl&lt;string|null&gt;
  [key: string]: AbstractControl&lt;any, any&gt;;
}

答案2

得分: 0

export interface CustomFormControls {
  [key: string]: AbstractControl<string, string>;
  name: FormControl<string|null>;
  content: FormControl<string|null>;
}
英文:
export interface CustomFormControls {
  [key: string]: AbstractControl&lt;string, string&gt;;
  name: FormControl&lt;string|null&gt;;
  content: FormControl&lt;string|null&gt;;
}

huangapple
  • 本文由 发表于 2023年3月7日 22:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定