admin管理员组文章数量:1290090
I'm trying to pare to boolean value in a if
.
I would like to do something like this:
value1 = false;
value2 = true;
if (value1 === value2) {
... Some code ...
}
In Java you can use Booleanpare(boolean a, boolean b)
, but I can't find something equal in TypeScript.
For context, Booleanpare(boolean a, boolean b)
returns:
- 0 if
a
is equal tob
, - a negative value if
a
is false andb
is true, - a positive value if
a
is true andb
is false.
Thanks for you help
Edited: to show the message I get
This condition will always return 'false' since the types 'true' and 'false' have no overlap
I'm trying to pare to boolean value in a if
.
I would like to do something like this:
value1 = false;
value2 = true;
if (value1 === value2) {
... Some code ...
}
In Java you can use Boolean.pare(boolean a, boolean b)
, but I can't find something equal in TypeScript.
For context, Boolean.pare(boolean a, boolean b)
returns:
- 0 if
a
is equal tob
, - a negative value if
a
is false andb
is true, - a positive value if
a
is true andb
is false.
Thanks for you help
Edited: to show the message I get
This condition will always return 'false' since the types 'true' and 'false' have no overlap
Share Improve this question edited May 7, 2024 at 11:38 aioobe 421k114 gold badges828 silver badges839 bronze badges asked Dec 19, 2020 at 14:40 JBDJBD 7411 gold badge15 silver badges30 bronze badges 9- What's wrong with what you wrote? – tehhowch Commented Dec 19, 2020 at 14:42
-
1
===
is how to pare two boolean values. – jonrsharpe Commented Dec 19, 2020 at 14:42 -
1
===
checks for equality.Boolean.pare
checks for ordering (returns less than zero ifa
isfalse
andb
istrue
, zero if they are equal or more than zero ifa
istrue
andb
is false - is that the behavior you're looking for? – Mureinik Commented Dec 19, 2020 at 14:42 -
3
I've taken the liberty of adding a description for
Boolean.pare
to your question since it seems you're being downvoted due to being misunderstood. – Etheryte Commented Dec 19, 2020 at 14:47 - 1 dw someone and I upvoted you so at least you won't lose reputation – Pauline Nemchak Commented Dec 19, 2020 at 14:57
3 Answers
Reset to default 5Javascript doesn't have a builtin that's parable to Java's Boolean.pare()
. In fact, the Boolean class has nearly nothing in it, outside the constructor, toString()
and valueOf()
.
If you want to replicate the functionality yourself, you can use the Number
constructor.
function booleanCompare(a: boolean, b: boolean) {
return Number(a) - Number(b);
}
This can be used to reach all possible outmings:
const Result = {
first: 0, // A: 0, B 0
second: 1, // A: 1, B 0
third: -1, // A: 0, B 1
forth: 2, // A: 1, B 1
get: (A: boolean, B: boolean) => (Number(A) - Number(B))
}
const f = () => {
const A = true;
const B = true;
switch(Result.get(A, B)) {
case Result.first:
// To something
break;
case Result.second:
// To something
break;
case Result.third:
// To something
break;
case Result.forth:
// To something
}
}
In TypeScript, you can directly pare boolean values using the strict equality operator (===). If you want to replicate the behavior of Boolean.pare(boolean a, boolean b) in TypeScript, you can write a simple function to achieve that:
function pareBooleans(a: boolean, b: boolean): number {
if (a === b) {
return 0;
} else if (a === false && b === true) {
return -1;
} else {
return 1;
}
}
const value1: boolean = false;
const value2: boolean = true;
const parisonResult: number = pareBooleans(value1, value2);
This function replicates the behavior of Boolean.pare(boolean a, boolean b) where:
- It returns 0 if a is equal to b.
- It returns a negative value if a is false and b is true.
- It returns a positive value if a is true and b is false.
本文标签: javascriptHow to compare 2 boolean values in TypeScriptStack Overflow
版权声明:本文标题:javascript - How to compare 2 boolean values in TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741489087a2381537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论