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
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.
TypeScript Version: 4.1.2
Search Terms: async functions, booleans, async return values, async booleans
Code
Expected behavior:
TypeScript should warn you that the expression evaluated in the
if
statement will always betrue
. 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 returntrue
: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:
The text was updated successfully, but these errors were encountered: