启用 fullTemplateTypeCheck 和 strictTemplates 的 Angular 9:类型上不存在属性

ar0*_*968 5 angular angular9

我有一个像这样的组件

import { Component } from '@angular/core';

interface ItemBaseType {
   foo: boolean;
}

interface ItemType extends ItemBaseType {
   bar: boolean;
}

@Component({
  selector: 'ac-foo',
  template: `<div *ngIf="item.bar">Hello</div>`,
})
export class FooComponent {
   public item: ItemType;
}
Run Code Online (Sandbox Code Playgroud)

我在 Angular 9 应用程序上启用了fullTemplateTypeCheck 和 strictTemplates,它在运行时引发异常ng serve

类型“ItemBaseType”上不存在属性“bar”。

bar存在于ItemType.

出了什么问题?

小智 1

在这种情况下,您可以使用 item?.bar 。我希望它有帮助。