Skip to content

Make overload resolution work correctly with string literal types #10523

Closed
@RyanCavanaugh

Description

@RyanCavanaugh

Our overload resolution with string literal types does not behave well. Today, we both issue incorrect errors due to selecting a "first" overload, and fail to catch errors due to falling back to a less-specific overload.

let type: any = "div";
// Error, can't convert HTMLAnchorElement to HTMLDivElement.
//  Where the heck did HTMLAnchorElement come from?
let elem1 = <HTMLDivElement>document.createElement(type);

let elem2 = document.createElement("div");
// Why is this not an error? Totally wrong event type specified.
elem2.addEventListener("click", (ev: ClipboardEvent) => {
    console.log(ev.clipboardData);
});

Typically we say "You should put your most general overload last", which is fine, except when invoking a function with any, you really want the most general overload to get selected. Instead we end up selecting the first overload, which is essentially random here (HTMLAnchorElement being alphabetically first is not a good reason to select it vs anything else).

I don't have any comprehensive fix here but we're wrong in both directions here and should look at what can be done.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions