generator-jhipster icon indicating copy to clipboard operation
generator-jhipster copied to clipboard

Angular: NG8107 warning when compiling frontend in list and detail view with referenced other entities

Open OmarHawk opened this issue 3 years ago • 2 comments

Overview of the issue

In the following line, we do produce a ? character right before the element accessor:

https://github.com/jhipster/generator-jhipster/blob/1668772197f1e5fbc21d43b07abfa833c2e66a2d/generators/entity-client/templates/angular/src/main/webapp/app/entities/list/entity-management.component.html.ejs#L149

This produces such warnings during frontend build:

[INFO] Warning: src/main/webapp/app/entities/stream-data/list/stream-data.component.html:137:108 - 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.
[INFO] 
[INFO] 137               <a [routerLink]="['/stream-router', streamData.routerid.id, 'view']">{{ streamData.routerid?.info }}</a>
[INFO]                                                                                                                ~~~~
[INFO] 
[INFO]   src/main/webapp/app/entities/stream-data/list/stream-data.component.ts:18:16
[INFO]     18   templateUrl: './stream-data.component.html',
[INFO]                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[INFO]     Error occurs in the template of component StreamDataComponent.

Another place where the same thing is done is this:

https://github.com/jhipster/generator-jhipster/blob/1668772197f1e5fbc21d43b07abfa833c2e66a2d/generators/entity-client/templates/angular/src/main/webapp/app/entities/detail/entity-management-detail.component.html.ejs#L91

Producing such a warning

[INFO] Warning: src/main/webapp/app/entities/stream-log/detail/stream-log-detail.component.html:32:108 - 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.
[INFO] 
[INFO] 32             <a [routerLink]="['/stream-data', streamLog.streamData.uuid, 'view']">{{ streamLog.streamData?.uuid }}</a>
[INFO]                                                                                                               ~~~~
[INFO] 
[INFO]   src/main/webapp/app/entities/stream-log/detail/stream-log-detail.component.ts:8:16
[INFO]     8   templateUrl: './stream-log-detail.component.html',
[INFO]                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[INFO]     Error occurs in the template of component StreamLogDetailComponent.
Motivation for or Use Case

Shouldn't throw such warning during build.

Reproduce the error

See references above. We should put the question mark only for nullable fields... but interestingly, this field seems to be nullable here and the compiler seems to be not able to recognize that...

Related issues
Suggest a Fix

We should put the question mark only for nullable fields...

JHipster Version(s)

7.9.2

JHipster configuration

Leaving it out. I already put a reference to the code lines causing the issue.

Entity configuration(s) entityName.json files generated in the .jhipster directory
Browsers and Operating System
  • [x] Checking this box is mandatory (this is just to show you read everything)

OmarHawk avatar Aug 08 '22 13:08 OmarHawk

Maybe related to https://github.com/angular/angular/issues/46918

OmarHawk avatar Aug 08 '22 13:08 OmarHawk

I saw this in the last couple of days too.

mraible avatar Aug 08 '22 16:08 mraible

@mshima I think we can merge this without waiting. Mentioned lines in the issue are wrapped with ngIf checks that make sure the fields are there. Even the fields can be null or undefined, because of the ngIf ?. can be removed here.

Here is an example, we can remove the question mark here.

<div *ngIf="post.blog"> <a [routerLink]="['/blog', post.blog.id, 'view']">{{ post.blog?.name }}</a> </div>

ertunga avatar Aug 15 '22 21:08 ertunga

@ertunga looks correct thanks for the example.

mshima avatar Aug 15 '22 21:08 mshima