Skip to content

Async functions that return booleans: un-awaited function calls are not flagged as always being truthy #41716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AleksandrHovhannisyan opened this issue Nov 28, 2020 · 2 comments

Comments

@AleksandrHovhannisyan
Copy link

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:

image

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:

@MartinJohns
Copy link
Contributor

Duplicate of #25330.

@AleksandrHovhannisyan
Copy link
Author

@MartinJohns My bad, closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants