Skip to content

Recursive functions are inferred to have return type any #3336

Closed
@fsoikin

Description

@fsoikin

Consider this code:

function fact( x: number, acc?: number ) {
    if (x <= 1) return acc;
    else return fact( x - 1, (acc||1)*x );
}

var res = fact( 5 );

(same thing on playground)

Here, fact is inferred to have type (number, number?) => any, and consequently res is inferred to have type any.
This is vaguely reminiscent of cases #1146, #475, and #523, but those deal with some esoteric circumstances, while this here is a textbook case of a tail-recursive algorithm. Should work.

Note: no, it doesn't have to do with optional parameter, I checked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions