question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

extended diagnostic for non-nullable optional is wrong

See original GitHub issue

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

core

Is this a regression?

No

Description

Having the case below produces the warning for optional chain operation. Because of ! it will always return a boolean to the input. Relates to the new feature https://github.com/angular/angular/pull/46686 https://github.com/angular/angular/issues/44870

Also can we have a angularCompilerOptions to disable this warnings?

First case:

optionalArrayElement: {a: 'value'}[] = [];
<button [disabled]="!optionalArrayElement[0]?.a"></button>

Second case:

enum MapKeys {
  A,
  B
}

 optionalMapElement: {[key: string]: {a: string}} = {[MapKeys.A] : {a: 'value'}};
<button [disabled]="!optionalMapElement[MapKeys.B]?.a"></button>

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-r3n3ec?file=src/app/app.component.ts

Please provide the exception or error you saw

warning 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.

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

Angular CLI: 14.1.0
Node: 16.15.1
Package Manager: npm 8.13.2
OS: win32 x64

Angular: 14.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
... service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1401.0
@angular-devkit/build-angular   14.1.0
@angular-devkit/core            14.1.0
@angular-devkit/schematics      14.1.0
@schematics/angular             14.1.0
rxjs                            7.5.6
typescript                      4.7.4

Anything else?

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:17
  • Comments:32 (11 by maintainers)

github_iconTop GitHub Comments

25reactions
e-ozcommented, Aug 10, 2022

An example of how to suppress this check right now (it’s not obvious from previous comments):


  "angularCompilerOptions": {
    "extendedDiagnostics": {
      "checks": {
        "optionalChainNotNullable": "suppress"
      }
    }
  }

10reactions
JoostKcommented, Jul 29, 2022

I’d also like to turn off this warning.


"angularCompilerOptions": {

    "strictNullChecks": false

  }

Please do not do this. strictNullChecks is not an Angular compiler option, and disabling the TypeScript option is definitely not recommended.

This particular rule can be disabled like any other extended diagnostic, as documented here: https://angular.io/extended-diagnostics#configuration. The rule name is optionalChainNotNullable, which is currently not yet included in the docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NG8102: Nullish coalescing not nullable - Angular
This diagnostic detects a useless nullish coalescing operator ( ?? ) characters in Angular templates. Specifically, it looks for operations where the input ......
Read more >
KT-36770 - YouTrack - JetBrains
In Spring Data JPA, the CrudRepository interface throws an IllegalArgumentException and not a NullPointerException for methods like Optional<T> findById(ID id) ...
Read more >
Improving non-nullable reference types handling - Cezary Piątek
The main thing that started getting on my nerves was diagnostic CS8618: Non-nullable field is uninitialized. Consider declaring as nullable.
Read more >
Understanding null safety - Dart programming language
It underpins optional parameters, the handy ?. null-aware operator, ... With sound non-nullable types, this program is flat out wrong and unsafe.
Read more >
Update your codebase to use nullable reference types
... and how to diagnose and fix possible null dereferencing issues. ... Enable type annotations; Attributes extend type annotations ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found