TypeScript can't infer correct type from while loop if while condition is given a promise #55300
Labels
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
π Search Terms
promise in while loop condition
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.2.0-dev.20230807#code/GYVwdgxgLglg9mABAZwDYFN0AcAUBbZALkTBDwCN0AnASmIAUq48Zl0AeKKkdAPkQDeAKESIq6KCCpIw6AO6JGzVhy49eOAIaIAvPzZQAKjDzo4IKDhw1d-TTjXoaAGkQEaNIQF8hQzcgBPSERQSFgERHQAD008LAwARmsGJhY2dmQuGDAAc35hAEgMKEQAN01UHmJMqmycxAAfRHAAE3RgbPQWoQK5AAsYDEQHbidBHoLNOU0YErRMXABWAAZVzwKC8sr0XUQAckW9ifJxTQBrHp8C8UlpMoqeb19-IIgQ8Gh4JGjY+PQAJmSilSKgyWVy+R6xXu22q4PqTVa7U63V6AyGWmmsxQGGwOBWaxshU2Dx2On2h2OpwuBSuNykSC2jy8QA
π» Code
π Actual behavior
In example2, TypeScript can't infer the type of
value
and that it should be now a string and notundefined
simply because the Promise that returns true in the while loop has been moved to the while condition instead of the body of the while loop.π Expected behavior
The expected behaviour should be that TypeScript should be able to infer that it is still a string at the return statement just like in example1
The text was updated successfully, but these errors were encountered: