I have a typescript function which looks like:
const doesContainProduct = () => {
if (cart.length >= 1 ) {
return true
} else {
return false;
}
}
I am referencing the above function like this:
if (doesContainProduct === true) {
return null;
}
And I am getting the following error:
TS2367: This condition will always return 'false' since the types '() => boolean' and 'boolean' have no overlap.
> 136 | if (doesContainProduct === true) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
137 | return null;
138 | } else {
139 | alert("Yes");
This looks very straightforward to me, but don't know why this is happening. Any explanation on this would be beneficial.
CodePudding user response:
Call the function.
if (doesContainProduct()) {
return null;
}
(also don't compare boolean values to true
and false
; just use them like the boolean values they are)
CodePudding user response:
You want to make a variable of the value of that functiont then you can get the value===>
const doesContainProduct = (cart) => {
cart = "1";
if (cart.length >= 1 ) {
return true;
} else {
return false;
}
}
let test = doesContainProduct();
if(test === true){
console.log("It is true")
}
else{
console.log("It is false")
}