Skip to content

Infer type of simple recursionΒ #55342

Closed
Closed
@rotu

Description

@rotu

πŸ” Search Terms

recursive, never, 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.

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

With recursive functions, it is often possible to infer the return type without function annotations.

πŸ“ƒ Motivating Example

// @target: ES2020
function factorial(x:bigint){
  if (x<=0n)
    return 1n
  return x*(factorial(x-1n))
}

function factorial2(x:bigint){
  if (x<=0n)
    return 1n
  return x*(factorial2(x-1n) as bigint)
}

Workbench Repro

πŸ’» Use Cases

TBD

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions