Skip to content

strictTemplates and strictNullChecks doesn't work well with optional chaining in templates #37619

@ajafff

Description

@ajafff

🐞 bug report

Affected Package

The issue is caused by package @angular/compiler?

Is this a regression?

?

Description

  • enable Ivy
  • enable strictTemplates
  • enable strictNullChecks
  • use an expression like foo?.bar.baz in a template
    • foo is nullable, bar is not
    • template compiler shows error on baz: Object is possibly 'undefined'.
  • note that the whole chain ends if foo is nullish (long short circuiting), therefore accessing baz is always safe
  • disabling strictNullChecks makes the compile error go away and everything functions as expected without runtime error (i.e. Angular's optional chaining also does long short circuiting)

🔬 Minimal Reproduction

https://stackblitz.com/edit/angular-ivy-pu8dk5

🔥 Exception or Error


`Object is possibly 'undefined'.`

🌍 Your Environment

Angular Version:


@angular/animations 9.1.11
@angular/common 9.1.11
@angular/compiler 9.1.11
@angular/core 9.1.11
@angular/forms 9.1.11
@angular/platform-browser 9.1.11
@angular/platform-browser-dynamic 9.1.11
@angular/router 9.1.11

Anything else relevant?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions