我尝试运行皮棉来修复错误。有人可以帮我如何解决这些错误。
在运行lint --fix之前
it('name field validity', () => {
let name = component.frmBusinessGroupEditor.controls['name'];
expect(name.valid).toBeFalsy();
let errors = {};
errors = name.errors || {};
expect(errors['required']).toBeTruthy();
name.setValue("test#");
errors = name.errors || {};
expect(errors['required']).toBeFalsy();
expect(errors['pattern']).toBeTruthy();
});
运行命令后
错误:类型'{}'不存在属性'required'。类型'{}'不存在属性'pattern'。
it('name field validity', () => {
const name = component.frmBusinessGroupEditor.controls.name;
expect(name.valid).toBeFalsy();
let errors = {};
errors = name.errors || {};
expect(errors.required).toBeTruthy();
name.setValue('test#');
errors = name.errors || {};
expect(errors.required).toBeFalsy();
expect(errors.pattern).toBeTruthy();
});
原因是您的errors
类型为{}
(空对象)。
[let errors = {};
应为let errors: {required?: boolean, pattern?: string} = {}
。
您也可以这样使用界面来执行此操作。
interface IErrors {
required?: boolean;
pattern?: string;
}
然后是let errors: IErrors = {};
写完这个答案后,我看到您正在分配errors = name.errors
,所以最好使用name.errors
的类型并使用它,而不是创建一个新的接口。