Skip to content

Angular flags valid null check as NG8107 for function calls #69609

Description

@hamfastgamgee

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

Don't known / other

Is this a regression?

No

Description

With optionalChainNotNullable enabled in tsconfig (the default in v22), the template compiler flags a valid and necessary ?. operator check on an attempt to call a possibly null or undefined function definition as NG8107 when the object with the function property is itself able to be null or undefined. (False positive)

Conversely, if you remove the ?. operator per the NG8107, the error is removed but you risk runtime exceptions. (False negative)

NG8107 is correctly applied or not applied as per the function property's type if the object it is on is non-nullable.

As you will see in the (perhaps overly exhaustive) minimal reproduction, the important trigger seems to be the (correct) ?. check on the object we're using. I'm not getting the runtime exception for the false negative cases to fire in the StackBlitz like happened in my production app, but that may be a StackBlitz limitation or a "me" limitation. :) I was going to include a section in the StackBlitz showing a non-nullable object with an attempt to call a null or undefined function without an ?. operator, but Typescript's compiler ate that one before we even reached the Angular errors and caused the whole project not to run (which I found surprising), but since it was also a "this is working as expected" case, I felt it was unnecessary to try to work around in the context of the reproduction.

As an aside, I had originally thought this was exclusive to signals because the non-signal objects in my situation were not causing the issue, but as the minimal reproduction indicates, the issue revolves around the nullability of the object holding the function definition and not whether signals are in use.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-z3bmjnus

Please provide the exception or error you saw

The standard NG8107 error text

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

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI       : 22.0.5
Angular           : 22.0.4
Node.js           : 22.22.3
Package Manager   : npm 10.8.2
Operating System  : linux x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 22.0.5            │ ^22.0.5           │
│ @angular/cli              │ 22.0.5            │ 22.0.5            │
│ @angular/common           │ 22.0.4            │ 22.0.4            │
│ @angular/compiler         │ 22.0.4            │ 22.0.4            │
│ @angular/compiler-cli     │ 22.0.4            │ 22.0.4            │
│ @angular/core             │ 22.0.4            │ 22.0.4            │
│ @angular/forms            │ 22.0.4            │ 22.0.4            │
│ @angular/platform-browser │ 22.0.4            │ 22.0.4            │
│ @angular/router           │ 22.0.4            │ 22.0.4            │
│ rxjs                      │ 7.8.2             │ ^7.8.1            │
│ typescript                │ 6.0.3             │ ^6.0.3            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions