Javascript 打字稿错误此条件将始终返回“真”,因为类型没有重叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53719693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript error This condition will always return 'true' since the types have no overlap
提问by alim1990
I having this condition on a form group:
我在表单组上遇到这种情况:
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")))
It contain 3 main conditions:
它包含3个主要条件:
- If a person aged 17, cannot be set to
infant - If a person is bigger than 40, he cannot be a
grandchild - If a person is less than 5 years, he should be
child,infant,grandchildorcousin.
- 如果一个人年满 17 岁,则不能设置为
infant - 如果一个人大于 40,他就不能成为
grandchild - 如果一个人不到 5 年,他应该是
child、infant、grandchild或cousin。
If one of these conditions is true, I will send an error message.
如果这些条件之一为真,我将发送错误消息。
The error I am receiving is:
我收到的错误是:
[ts] This condition will always return 'true' since the types '"Child"' and '"Infant"' have no overlap. [2367]
[ts] 此条件将始终返回 'true',因为类型 '"Child"' 和 '"Infant"' 没有重叠。[2367]
On this part of the ifcondition`:
在这部分if条件`:
|| this.frType!="Infant" || this.frType!="Grandchild" || this.frType!="Cousin")))
I am using the exact condition in a different component, and it does not show an error.
我在不同的组件中使用了确切的条件,它没有显示错误。
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")))
Here is how I am calculating the age in both components:
这是我如何计算两个组件中的年龄:
let timeDiff = Math.abs(Date.now() - this.formGroup.controls['dob'].value);
let age = Math.floor((timeDiff / (1000 * 3600 * 24))/365);
回答by CertainPerformance
Consider the standalone expression:
考虑独立表达式:
(this.frType!="Child" || this.frType!="Infant")
If frTypeis Child, the second part will be true, so the expression will evaluate to true. If frTypeis Infant, then the first part will be true, so the expression will evaluate to true. If frTypeis neitherChildnor Infant, then the first part will be true, and the expression will, again, evalute to true- the logic is faulty, it'll always resolve to true.
如果frType是Child,则第二部分将为真,因此表达式的计算结果为true。如果frType是Infant,则第一部分将为真,因此表达式的计算结果为true。如果既不frType是也不Child是Infant,那么第一部分将为真,表达式将再次计算为true- 逻辑有问题,它将始终解析为true。
(If you add additional ||conditions for Grandchildand Cousin, the same thing keeps happening - it'll always resolve to true)
(如果你||为Grandchildand添加额外的条件Cousin,同样的事情会不断发生 - 它总是会解析为true)
Either use &&instead:
要么&&改用:
|| (age<=5 && (
this.frType!="Child"
&& this.frType!="Infant"
&& this.frType!="Grandchild"
&& this.frType!="Cousin"
))
Or, to make the logic easier to follow, you might consider using an array, and use .includes:
或者,为了使逻辑更易于遵循,您可以考虑使用数组,并使用.includes:
const kidsFiveAndUnder = ['Child', 'Infant', 'Grandchild', 'Cousin'];
// ...
|| (age <= 5 && !kidsFiveAndUnder.includes(this.frType))

