Skip to content

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

Closed
@hospotho

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

Metadata

Metadata

Assignees

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