TypeScript 错误 - 此条件将始终返回'false',因为类型'boolean'和'string'没有重叠



我在ng-container中添加ngIf条件,如果条件满足,则display the component

我面临的问题是我得到错误

This condition will always return 'false' since the types 'boolean' and 'string' have no overlap.

当我将条件设置为(当我使用not operator after &&(时

<ng-container *ngIf="data.dataClass === 'Dokes'
&& !data.dataSubclass === 'Land'"> ------------------>   NOT OPERATOR

但当我删除&& !data.dataSubclass === 'Land'中的!运算符时,代码运行良好

我不知道为什么它会给我这个错误。我想添加!操作符作为I want to display component when data.dataSubclass is not equal to 'Land'

.html文件

<div>
<ng-container *ngIf="data.dataClass === 'Dokes' 
&& !data.dataSubclass === 'Land'">-----------------------------> ERRORRR
!----- BELOW IS THE COMPONENT TO DISPLAY IF ABOVE CONDITION IS TRUE---------!
<money-property name="Pika" [model]="data.pika"
[editMode]="!!dataEditService.editMode">
<dl>
<dd *ngFor="let d of data.pika; index as i">
<edit-link dataClass="Dokes" 
[(selectedElement)]="data.pika[i]" [editMode]="!!dataEditService.editMode"></edit-link>
</dd>

</dl>
</money-property>
</ng-container>
</div>

添加!会抛出您试图实现的逻辑,因为它会否定其右侧的值

&& !data.dataSubclass === 'Land'

相当于

&& (!data.dataSubclass) === 'Land'

这当然没有意义——布尔值永远无法与'Land'相比。您需要:

&& !(data.dataSubclass === 'Land')

但更明确的方法是:

&& data.dataSubclass !== 'Land'

最新更新