TypeScript Version: 3.1
Search Terms: boolean void truthy falsy if while do for not
An expression of type 'void' cannot be tested for truthiness
Code
Consider this code:
function existsAndIsCool(fileName: string) {
fs.exists(fileName, exists => {
return exists && isCool(fileName);
});
}
if (existsAndIsCool("myFile")) {
console.log("Cool!");
} else {
console.log("Not cool :(");
}
Expected behavior: The "Cool!" branch is actually unreachable because existsAndIsCool returns void, not boolean. I should have been warned about this at some point.
Actual behavior: No error, never cool 😢
Playground Link: Link
Related Issues:
PR #26234
#7256
#10942
#7306
TypeScript Version: 3.1
Search Terms: boolean void truthy falsy if while do for not
An expression of type 'void' cannot be tested for truthiness
Code
Consider this code:
Expected behavior: The "Cool!" branch is actually unreachable because
existsAndIsCoolreturnsvoid, notboolean. I should have been warned about this at some point.Actual behavior: No error, never cool 😢
Playground Link: Link
Related Issues:
PR #26234
#7256
#10942
#7306