extended diagnostic for non-nullable optional is wrong
See original GitHub issueWhich @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:
- Created a year ago
- Reactions:17
- Comments:32 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
An example of how to suppress this check right now (it’s not obvious from previous comments):
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.