Skip to content

false positive for NG8107 when used inside @for #70085

Closed

Description

@robertIsaac

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

Yes

Description

right now after the migration to angular 22 we are getting a lot of warnings for a false positive of NG8107 error when used inside a template @for when strict is enabled (I don't understand why in angular 22 by default it's not enabled)

Please provide a link to a minimal reproduction of the bug

https://github.com/robertIsaac/optional-chain-angular-reprod

Please provide the exception or error you saw

▲ [WARNING] NG8107: NG8107: The left side of this optional chain operation does not include 'null' or 'undefined' in its type, therefore the '?.' operator can be replaced with the '.' operator. Find more at https://v22.angular.dev/extended-diagnostics/NG8107 [plugin angular-compiler]

    src/app/app.html:1:38:
      1 │ @for (item of type().one?.two?.three?.array; track item.name) {
        ╵                                       ~~~~~

  Warning occurs in the template of component App.

    src/app/app.ts:17:15:
      17 │   templateUrl: './app.html',
         ╵                ~~~~~~~~~~~~


▲ [WARNING] NG8107: NG8107: The left side of this optional chain operation does not include 'null' or 'undefined' in its type, therefore the '?.' operator can be replaced with the '.' operator. Find more at https://v22.angular.dev/extended-diagnostics/NG8107 [plugin angular-compiler]

    src/app/app.html:1:38:
      1 │ @for (item of type().one?.two?.three?.array; track item.name) {
        ╵                                       ~~~~~

  Warning occurs in the template of component App.

    src/app/app.ts:17:15:
      17 │   templateUrl: './app.html',
         ╵                ~~~~~~~~~~~~

Please provide the environment you discovered this bug in (run ng version)

ngular CLI       : 22.1.3
Angular           : 22.1.0
Node.js           : 24.15.0
Package Manager   : npm 11.17.0
Operating System  : darwin arm64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 22.1.3            │ ^22.1.3           │
│ @angular/cli              │ 22.1.3            │ ^22.1.3           │
│ @angular/common           │ 22.1.0            │ ^22.1.0           │
│ @angular/compiler         │ 22.1.0            │ ^22.1.0           │
│ @angular/compiler-cli     │ 22.1.0            │ ^22.1.0           │
│ @angular/core             │ 22.1.0            │ ^22.1.0           │
│ @angular/forms            │ 22.1.0            │ ^22.1.0           │
│ @angular/platform-browser │ 22.1.0            │ ^22.1.0           │
│ @angular/router           │ 22.1.0            │ ^22.1.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 6.0.3             │ ~6.0.2            │
│ vitest                    │ 4.1.10            │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Activity

  1. added
    area: compiler
    on Aug 5, 2026
  2. added this to the needsTriage milestone on Aug 5, 2026
  3. added this to the needsTriage milestone on Aug 5, 2026
  4. JeanMeche commented on Aug 5, 2026

    @JeanMeche
    Member

    Looks we are adding some parenthesis in the TCB which changing the semantics.

  5. robertIsaac commented on Aug 5, 2026

    @robertIsaac
    ContributorAuthor

    @JeanMeche I dig deeper into the strict: true it seems that default now should be true according to announcing typescript 6
    but in the provided repo if you removed it then the warning is removed
    it seems that angular compiler doesn't respect the new strict default value
    this was the most confusing part for me, because when I tried to reproduce the error in a new project it wasn't showing and took me a while to see that the only different was strict: true in the real project vs none in the reprod one

  6. AyushDubey23 commented on Aug 5, 2026

    @AyushDubey23

    Thanks for the report! As @JeanMeche pointed out, the generated TCB's for...of loop applied a trailing !, causing TypeScript to incorrectly narrow intermediate safe-navigation receivers and triggering false NG8107 warnings.

    This is fixed by emitting the original expression before the for...of loop, preserving the correct nullable types for extended diagnostics.

  7. JeanMeche commented on Aug 5, 2026

    @JeanMeche
    Member

    @robertIsaac This is actually an interesting feedback and worth of a distinct issue: Our diagnostics seems to only report this issue (and probably others) when the option is explicitly set to true.

  8. self-assigned this
    on Aug 5, 2026
  9. JeanMeche commented on Aug 5, 2026

    @JeanMeche
    Member

    #70090 would address the strictness issue.

  10. robertIsaac commented on Aug 5, 2026

    @robertIsaac
    ContributorAuthor

    I'm very happy to hear that my investigation helped to pinpoint two issues
    Thanks @JeanMeche for the quick fixes

  11. added a commit that references this issue on Aug 7, 2026
    d7b03f5
  12. added a commit that references this issue on Aug 7, 2026
    76dff30
  13. angular-automatic-lock-bot commented on Oct 19, 2026

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilercompiler: template type-checkingstate: confirmed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions