当tsconfig.json中的editorOptions.strict为true时,嵌套* ngIf =“ false”将创建“错误TS2532:对象可能为'undefined'”

时间:2020-03-17 06:58:16

标签: angular typescript

我不知道为什么简单的*ngIf="false"会导致error TS2532: Object is possibly 'undefined'.


tsconfig.json

"compilerOptions": {
    ...
    "strict": true,


app.component.ts

export class AppComponent {
  user? : User;
}

interface User {
  name: string;
}

app.component.html

<div *ngIf="user">
  <div>{{user.name}}</div> <!-- OK -->
  <div *ngIf="true">{{user.name}}</div>  <!-- OK -->
  <div *ngIf="false">{{user.name}}</div>  <!-- error TS2532: Object is possibly 'undefined'. -->
</div>

全角项目:https://gitlab.com/zchlian/angular-test-strict/

0 个答案:

没有答案