【发布时间】:2019-05-12 04:20:42
【问题描述】:
我在表单组上遇到这种情况:
if((age>17 && (this.frType=="Infant"))
|| (age>40 && this.frType=="Grandchild")
|| (age<=5 &&
(this.frType!="Child"
|| this.frType!="Infant"
|| this.frType!="Grandchild" || this.frType!="Cousin")))
它包含三个主要条件:
- 如果是17岁的人,不能设置为
infant - 如果一个人大于40,他不能是
grandchild - 如果一个人小于5岁,他应该是
child、infant、grandchild或cousin。
如果这些条件之一为真,我将发送一条错误消息。
我收到的错误是:
[ts] 此条件将始终返回“真”,因为类型 “儿童”和“婴儿”没有重叠。 [2367]
关于if条件的这一部分:
|| this.frType!="Infant" || this.frType!="Grandchild" || this.frType!="Cousin")))
我在不同的组件中使用了确切的条件,它没有显示错误。
if((age>17 && (this.family_relation_type=="Infant"))
|| (age>40 && this.family_relation_type=="Grandchild")
|| (age<=5 &&
(this.family_relation_type!="Child" ||
this.family_relation_type!="Infant" ||
this.family_relation_type!="Grandchild" ||
this.family_relation_type!="Cousin")))
这是我计算两个部分的年龄的方法:
let timeDiff = Math.abs(Date.now() - this.formGroup.controls['dob'].value);
let age = Math.floor((timeDiff / (1000 * 3600 * 24))/365);
【问题讨论】:
-
在进行隐式值比较时发生。使用“===”和“!==”代替“==”和“!=”。
标签: javascript angular typescript typescript2.0