Skip to content

Overload resolution #32325

Description

@ttftw

TypeScript Version: 3.4.0-dev.20190710

Search Terms: overload

Code

interface Emittable {
    on(event: string, listener: Function): this;
    on(event: 'ready' | 'disconnect', listener: () => void): this;
    on(event: 'messageReactionAdd' | 'messageReactionRemove', listener: (message: string, emoji: string, userID: string) => void): this;
}

const client: Emittable;

client.on('messageReactionAdd', () => {});

Expected behavior: TS to detect the types from the declared interface based on signature.

Actual behavior: When on(event: string, listener: Function) is declared first, TS does not read the declared types from the interface overload for client.on('messageReactionAdd', () => {});, instead IntelliSense uses either the default overload, or an incorrect signature and the TS server complains about expected types. If I move the default overload to the end of the list, TS reads the types correctly.

This seems to be caused by having a pipe | between the expected strings in the signature. client.on('ready') works fine regardless of if the default overload is listed first or not, but 'messageReactionAdd' | 'messageReactionRemove' only works correctly if the default overload is listed last.

Playground Link: https://www.typescriptlang.org/play/index.html#code/JYOwLgpgTgZghgYwgAgKIFthjHARgGxQG8BYAKGWQHsQAKCANwnAC5kBnMKUAcwBpk+YJ2bQ2AMQCuIBGGA0AlGzAALYQG5ylGvSatkAIigQ4AEwCeBgUJEgxyWugjt2cHhDaduIHguQBeAD5kBipgUyVkVQ0tajpYykZmMDYDJxc3CAAlE1l5EABBU1MDZAAfQ3TXdxzEORoc9ComKwTBYUg7KDZHZ2qPDi5eAQgmgCtgTyGfAUl2aABJABEp718A4NDw2Mjo9k0yAF9ycgQaTmQEIWS2DCwcAggD0+vwADodNL7M2ryaIpKAlofiCyCIhwUByAA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions