You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Language service errors on possibly null binding when it's already checked #18248
[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
This will not be addressed until 6.0 as it will require a breaking change to the NgIf declaration that will not be ready in time for 5.0.
The template compiler does not know the runtime behavior of directives, including something as fundamental as *ngIf, therefore, there is currently no typing relationship that can be derived for data inside the template. This is similar to, for example, if you would have written the following in TypeScript:
here (turn on strict null option to see the error).
This will report that data could possibly be undefined in lambda because proving otherwise requires knowledge of the runtime behavior of ngIf.
To do this we could change the declaration of NgIf to something like what is implied by this playground (turn on strict null checks and see that d is inferred to be of type string).
Hi, does this also apply to the case of type narrowing inside the if-condition itself, or is that a separate issue?
class Animal { }
class Bird extends Animal { public wingCount: number; }
@Component({
template: `<div *ngIf="isBird(animal) && (animal.wingCount > 2)"><p>This is one crazy bird!</p></div>`
})
export class TestComponent {
@Input() animal: Animal;
isBird(a: Animal): a is Bird { return a instanceof Bird; }
}
In the current language service, the reference to animal.wingCount would be an error ("'Animal' does not contain such a member"), even though of course the AOT compiler gets through it just fine.
I don't think it's fixed on the templates, at least -- see my previous comment. I don't run the language service directly (I guess via tsc?) but in VS Code (Salsa), I still get warnings in statements that would compile cleanly in non-template (.ts file) context. In the above example, *ngIf="isBird(animal) && (animal.wingCount > 2) would have a red squiggle under animal.wingCount because wingCount is only defined on Bird and the language service doesn't treat isBird as narrowing the type, even though the actual compiler does.
Would it be better to re-open this issue, or to open a new issue specific to errors in the template itself?
I am having the same issue with version 7. I am currently adding ! in front of expressions even though I am using *ngIf. I think this hack is not clean.
Reacted by Andrei Balasescu
angular-automatic-lock-bot commented on Sep 14, 2019
Activity
cyrilletuzi commentedon Aug 18, 2017
@chuckjaz Some news about this issue ?
chuckjaz commentedon Aug 18, 2017
This will not be addressed until 6.0 as it will require a breaking change to the
NgIfdeclaration that will not be ready in time for 5.0.The template compiler does not know the runtime behavior of directives, including something as fundamental as
*ngIf, therefore, there is currently no typing relationship that can be derived fordatainside the template. This is similar to, for example, if you would have written the following in TypeScript:here (turn on strict null option to see the error).
This will report that
datacould possibly be undefined in lambda because proving otherwise requires knowledge of the runtime behavior ofngIf.To do this we could change the declaration of
NgIfto something like what is implied by this playground (turn on strict null checks and see thatdis inferred to be of typestring).This would be similar to rewriting the above as:
here
The new type checker is scheduled for 5.1 but this will not be included in it. As this is a breaking change it cannot happen till 6.0.
cyrilletuzi commentedon Aug 18, 2017
Thanks for the detailed answer.
thw0rted commentedon Feb 27, 2018
Hi, does this also apply to the case of type narrowing inside the if-condition itself, or is that a separate issue?
In the current language service, the reference to
animal.wingCountwould be an error ("'Animal' does not contain such a member"), even though of course the AOT compiler gets through it just fine.cyrilletuzi commentedon Apr 14, 2018
Closing this as there are no errors anymore.
But for info, now it seems there is the opposite problem: object is not detected as potentially
nullwhen there is no*ngIf.thw0rted commentedon Apr 16, 2018
I don't think it's fixed on the templates, at least -- see my previous comment. I don't run the language service directly (I guess via
tsc?) but in VS Code (Salsa), I still get warnings in statements that would compile cleanly in non-template (.tsfile) context. In the above example,*ngIf="isBird(animal) && (animal.wingCount > 2)would have a red squiggle underanimal.wingCountbecausewingCountis only defined onBirdand the language service doesn't treatisBirdas narrowing the type, even though the actual compiler does.Would it be better to re-open this issue, or to open a new issue specific to errors in the template itself?
nathanosdev commentedon Jun 23, 2018
@thw0rted The plugin for VSCode is using an older version of the Angular language service, so it would be better to open an issue here instead: https://github.com/angular/vscode-ng-language-service
See here: https://marketplace.visualstudio.com/items?itemName=Angular.ng-template
fetis commentedon Jul 2, 2018
@cyrilletuzi I'm using the language service v6.0.6 in WebStorm and has this issue.
inmogr commentedon Nov 27, 2018
@chuckjaz im having the same issue here
Could you please assist?
I'm using Angular 7.0.5 another note my
objectis of typeType | undefinedstill the error is showing asyoosif0 commentedon Mar 19, 2019
I am having the same issue with version 7. I am currently adding
!in front of expressions even though I am using *ngIf. I think this hack is not clean.angular-automatic-lock-bot commentedon Sep 14, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.