Skip to content

Overloads shouldn't differ by the return type of a function type. #571

@vilicvane

Description

@vilicvane

If we define an interface:

interface IHandler {
    (): number;
    (): string;
}

There would be error "overloads cannot differ only by return type", which is reasonable.

However, when it comes to some situation like this:

interface IHandler<T> {
    (processer: (str: string) => string): void;
    (processer: (str: string) => number): void;
}

The compiler would treat (str: string) => string and (str: string) => number as two incompatible types, which is also reasonable but not useful enough.

I would suggest to use parameter types to defer and use return type to do type checking only.

The real situation could be Promise<T>.prototype.then, because it accepts null as parameter, if one of the two arguments is null, it will skip the signature we would want it to match and adopts to something wrong. We may add an overload with any as the return type of its function type, but this would mute type checking.

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