Skip to content
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

Update when we can infer void from a function body #468

Closed
lucacasonato opened this issue May 13, 2024 · 3 comments · Fixed by #470
Closed

Update when we can infer void from a function body #468

lucacasonato opened this issue May 13, 2024 · 3 comments · Fixed by #470
Labels
bug Something isn't working fast check

Comments

@lucacasonato
Copy link
Member

lucacasonato commented May 13, 2024

Right now we sometimes incorrectly infer the function return type due to our return statement analysis. The only cases where we can safely infer void is when:

  • we are in a function declaration, there are no returns with value
  • or when we are in a function expression or arrow function expression and either:
    • a single return statement without value is anywhere in the body
    • the body is empty

Notably, we can not infer a function expression from expressions such as () => { n() }, because the return type may be never if n() returns never.

Sync functions: https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXxAA8sMAKASgC55UQA3EGAbgCgWiAHHGDJNTXPigV4AbxbxJBYmXKsAvm07deKdNjzwARiPFT4cDMhioWi9oS48 6wfDC6JUrInikA5hBxaoEACoAFlgAzuRiTvqSRCQUrPqK obGpubK1moCmsCO mZKlir2eMG8CAC8bmFlAHzh tGyCvlWvGDFqvAVIjV1UkkmeRYtRagl8O6dlZ21es6uHl4 -kGhvZHSMXIR8Al9IEYDqQXWbaO8AZPdM9uDaa3t8FiTGRqoOVINsbfH92fwAFbPfivd6SfopZqFU5jADWQNseFBj3mnm8vkCITCs3Wny28W24O w2hvAg8MybyxN3kQA
Async functions: https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXxAA8sMAKASgC55UQA3EGAbgCgWiAHHGDeKAZwCe6JGky58UCvADeLeAoLEy5VgF82nbrwHCwo9NjzwARtLmL4cDMhioWG9oS48 QkSkMT4Yc-MVYiPCkAOYQOCZQEAAqABZY-OSy-pYKRCQUrJYalta29o5arroeYkb4wH6WDprO2j54-LwIALxuesFJLQB8yZbpKuq1LrxgjbxBbSX60j19inl2NU4jDahN8CHwU 4zXb0WAUGh4ZEx8YnzqUoZqinwOQsgNkuFda5j67yx2 0iswd7ssiqNxvAsL9pgZxHgqooBplge9QV94AArSG7aHlOEKRYFYb1T4bADWmI6nhhqFx4OOYQiUTiCSSh2uCLu2Xu KRq2JvAg5NKXlhLLYaiAA

@lucacasonato lucacasonato added fast check bug Something isn't working labels May 13, 2024
@marvinhagemeister
Copy link
Contributor

For completeness: This requires to traverse into every block-like scope to potentially find return types:

export function foo() {
  for (let i = 0; i < 10; i  ) {
    if (i === 10) {
      return 42
    }
  }
}

@lucacasonato
Copy link
Member Author

We already do that 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fast check
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants