You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Function truthy check doesn't apply if the function is in a boolean expression #37598
Expected behavior:
All three if statements should throw a This condition will always return true since the function is always defined. Did you mean to call it instead? error.
Actual behavior:
Only the first statement throws the error.
The situation depicted by this particular playground has been actually fixed~ in 4.2.x: TS playground. It was most likely improved by #41599
However, some cases still don't work, like this one: TS playground
constthing={isBeautiful(){returntrue;},isBiggerThanBreadBox(){returnfalse;},isYellow(){returnfalse;},};if(thing.isBeautiful||thing.isYellow){console.log("Hi");}// Should error like aboveif(thing.isBeautiful()&&(thing.isBiggerThanBreadBox||thing.isYellow)){console.log("Hi");}
Aaron Munger (@amunger) this is definitely related but I'd say that perhaps it's worth creating a separate issue about this (if there is none about it already). I have a PR open that aims to fix this but it only deals with binary(like) expressions - I didn't touch unary expressions anyhow there.
The uncalled function check is intentionally very limited, since there are a lot of cases where you might be checking a function truthiness on purpose. For the case of if (!x) it's reasonable to not see an x in the body of the function, and we didn't want you to have to write !!!x to get around the checking error, so not including that case was intentional.
Activity
Andarist commentedon Sep 13, 2022
The situation depicted by this particular playground has been actually fixed~ in 4.2.x: TS playground. It was most likely improved by #41599
However, some cases still don't work, like this one: TS playground
amunger commentedon Dec 9, 2022
Is the fact that simply negating the check causes the warning to disappear covered by this issue?
https://www.typescriptlang.org/play?#code/MYewdgzgLgBFAWBLMBzGBeGBvAsAKBhkQgCEBTAQwFcpEAzKgGwAoBKbfQwgJzKiu5g43KmQDcnGAF98UiXnz0YzBMhQA6YuWq0GjdrgIxQkEIzLrGIFMwBEACUS3W8mQrwB6DzADK8EEwAJjBk3Nwg3DCMiADWZDAUAEYgAG5kinTKAISqqJqklDT0TAaSJhBmFlY2Dk4uskA
Andarist commentedon Dec 9, 2022
Aaron Munger (@amunger) this is definitely related but I'd say that perhaps it's worth creating a separate issue about this (if there is none about it already). I have a PR open that aims to fix this but it only deals with binary(like) expressions - I didn't touch unary expressions anyhow there.
RyanCavanaugh commentedon Dec 9, 2022
The uncalled function check is intentionally very limited, since there are a lot of cases where you might be checking a function truthiness on purpose. For the case of
if (!x)it's reasonable to not see anxin the body of the function, and we didn't want you to have to write!!!xto get around the checking error, so not including that case was intentional.Improve uncalled function check related to parenthesized and binary e…