In the scenario where I am working with a template that includes a boolean called readOnly
and an array known as arrayOfStuff
:
<span *ngIf="!readOnly && arrayOfStuff && arrayOfStuff.length">Hey</span>
When running eitherng build --prod
orng serve --prod
, an error is thrown:
ERROR in /Development/project/src/$$_gendir/app/components/thing/thing.component.ngfactory.ts (767,11): Type 'number' is not assignable to type 'boolean'.
However, if I remove the boolean check, the code works perfectly fine:
<span *ngIf="arrayOfStuff && arrayOfStuff.length">Hey</span>
Alternatively, if I specifically compare the length of arrayOfStuff
as a number, it also resolves the issue:
<span *ngIf="!readOnly && arrayOfStuff && (arrayOfStuff.length >0)">Hey</span>
Why does checking against the falsy value of arrayOfStuff.length
work when determining the existence of an object, but not when evaluating a boolean value together with it?
It is important to note that this issue only occurs during production builds using ng build --prod
or ng serve --prod
. It does not show up in the development environment.
To clarify - although the problem can be worked around, understanding why direct checks on arrayOfStuff.length
behave differently in these instances is crucial.
Using Angular v4 and @angular/cli v1.0.1.