Description
TypeScript Version: 4.1.2
Search Terms: async functions, booleans, async return values, async booleans
Code
const isBroken = async () => false;
if (isBroken()) {
console.log('asd')
}
Expected behavior:
TypeScript should warn you that the expression evaluated in the if
statement will always be true
. See my explanation of this behavior here.
If you remove the function call and just do if (isBroken)
, TypeScript will rightly complain that the expression will always return true
:
That's because functions are truthy objects, so when they're coerced to booleans, they're always true. Likewise, an async function returns a Promise object implicitly, and that's always truthy. So TypeScript should really flag that as an error, but it doesn't.
Actual behavior:
TypeScript does not complain. The code can lead to bugs, but it's something that can be checked statically before the code is ever compiled.
Playground Link:
https://www.typescriptlang.org/play?#code/MYewdgzgLgBAlhAQgJxAawKZhgXhgQwgE8xgYAKASlwD4YAzfAGwgwG4AoDueihFdFmoBvDjBihIIJhgB0TEAHNyAckIATFZQ4BfIA
Related Issues: