Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
OmarHawk opened this issue Aug 8, 2022 · 4 comments · Fixed by #19427
Closed
1 task done
Labels
area: bug 🐛 $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: angular $100 https://www.jhipster.tech/bug-bounties/
Milestone

Comments

@OmarHawk
Copy link
Contributor

OmarHawk commented Aug 8, 2022

Overview of the issue

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

<a [routerLink]="['/<%= relationship.otherEntity.entityPage %>', <%= entityInstance %>.<%= relationshipFieldName %>.<%= relationship.otherEntity.primaryKey.name %>, 'view']">{{ <%= entityInstance %>.<%= relationshipFieldName %>?.<%= otherEntityField %> }}</a>

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:

<a [routerLink]="['/<%= otherEntityStateName %>', <%= entityInstance + '.' + relationshipFieldName + '.' + relationship.otherEntity.primaryKey.name %>, 'view']">{{ <%= entityInstance %>.<%= relationshipFieldName %>?.<%= otherEntityField %> }}</a>

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
  • Checking this box is mandatory (this is just to show you read everything)
@OmarHawk
Copy link
Contributor Author

OmarHawk commented Aug 8, 2022

Maybe related to angular/angular#46918

@mraible
Copy link
Contributor

mraible commented Aug 8, 2022

I saw this in the last couple of days too.

@mraible mraible added theme: angular $100 https://www.jhipster.tech/bug-bounties/ $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ and removed theme: undefined labels Aug 8, 2022
ertunga pushed a commit to ertunga/generator-jhipster that referenced this issue Aug 15, 2022
remove unnecessary optional properties from angular templates that cause warnings in the console

Fix jhipster#19373
ertunga added a commit to ertunga/generator-jhipster that referenced this issue Aug 15, 2022
remove unnecessary optional properties from angular templates that cause warnings in the console

Fix jhipster#19373
@ertunga
Copy link
Contributor

ertunga commented Aug 15, 2022

@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>

@mshima
Copy link
Member

mshima commented Aug 15, 2022

@ertunga looks correct thanks for the example.

@DanielFran DanielFran added this to the 7.9.3 milestone Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: bug 🐛 $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: angular $100 https://www.jhipster.tech/bug-bounties/
Projects
None yet
5 participants