Skip to content

Dissallow same parameter signature in overloads  #13612

Closed
@tinganho

Description

@tinganho

I think the below overloads are ambiguous:

namespace Greet {
    export function sayHi(): number
    export function sayHi(): string
    export function sayHi(): string | number {
        if (Math.random() > 0.5) {
            return '';
        }
        else {
            return 1;
        }
    }
}

// What 
typeof Greet.sayHi(); // What is the type?

Since, we are selecting a return type based on the parameter signature. But, what if the parameter signature is the same on multiple overloads? Nowadays, if the checker cannot find an appropriate overload it takes the first one. I think this is quite an unsafe approach. In the above example, it is clear that sayHi() cannot be narrowed to either string or number based on parameter signature selection, since there is only one parameter signature (), and thus should have the return type string | number only.

What I propose, is to error on the overload that has the same parameter signature:

namespace Greet {
    export function sayHi(): number // Error: Multiple same parameter signature on overload
    export function sayHi(): string // Error: Multiple same parameter signature on overload
    export function sayHi(): string | number {
        if (Math.random() > 0.5) {
            return '';
        }
        else {
            return 1;
        }
    }
}
``

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