我有一个这样的常量:
const defaultInfo: FormGroup = this.fb.group({
id: 1,
name: qian,
amount: 123,
})
并且我想限制FormGroup内部的类型,所以我这样写:
interface InfoInterface {
id: number,
name: string,
amount: number,
}
interface InfoFormGroup extends FormGroup {
value: InfoInterface
}
const defaultInfo: InfoFormGroup = this.fb.group({
id: 1,
name: qian,
amount: 123,
})
显然它不起作用,因为无论我将Info Interface的属性更改为什么,都没有错误,为什么?如何限制 ts 中 FormGroup 的值类型
限制ts中FormGroup的值类型
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我认为它应该对您有帮助:https://angular.io/guide/typed-forms
基本上,您需要使界面具有表单控件而不是普通类型:
interface InfoInterface { id: FormControl<number>, name: FormControl<string>, amount: FormControl<number> } const info = new FormGroup<InfoInterface>({ id: new FormControl(0), name: new FormControl(''), amount: new FormControl(0) });请记住,它是在 Angular 14 中引入的,因此它在以前的版本中不起作用