Skip to content

Inference of recursive arrow function's return type is any #58100

Closed

Description

@hospotho

🔎 Search Terms

recursive function arrow function

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.4#code/PTAEGMHsDsGcBdQDNKQFygBQEpQF4A+UAQ2gE8AoKORFSfLXQ0AbwtFAEsksBZY+AAsAdACdSAE0gBbHKCIAGYQFZcbDh1EBTeAFdR0ZKhztQAX1Pa9B0AooWKIZLujh4nGKABGxUTgzQutJeWqIUSC5uHoY+fmqm3HwCIuLQUrK4iirxGqBW+jG+JhwWmjoFtvYUQA

💻 Code

// const foo: () => any
const foo = () => {
  if (Math.random() > 0.5) {
    return foo()
  }
  return 0
}

// function bar(): number
function bar() {
  if (Math.random() > 0.5) {
    return bar()
  }
  return 0
}

🙁 Actual behavior

const foo: () => any
'foo' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.

🙂 Expected behavior

The inference should be const foo: () => number, even this inference is happened before the arrow function assign to foo

Additional information about the issue

No response

Activity

  1. MartinJohns commented on Apr 7, 2024

    @MartinJohns
    Contributor

    Duplicate of #53978. Used search terms: recursive any in:title

  2. hospotho commented on Apr 7, 2024

    @hospotho
    Author

    Duplicate of #53978. Used search terms: recursive any in:title

    They mention a pull request (#53995) related to regular function with self tail calls. Maybe I can update the title to "Self tail calls of arrow function were not ignored"?

  3. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions