Closed
Description
π 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.
-
If all return statements have inferrable type, it would be nice to use their union as the return type of a function (even if that function is recursive)
-
Similar to Ignore self tail calls when collecting the return type of a functionΒ #53995, you can assume in the body of a recursive function that any recursive call with unchanged argument types returns type
never
.
π 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)
}
π» Use Cases
TBD