Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union type interaction with overloads #1883

Closed
danielearwicker opened this issue Feb 2, 2015 · 2 comments
Closed

Union type interaction with overloads #1883

danielearwicker opened this issue Feb 2, 2015 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@danielearwicker
Copy link

Where there are overloads of a method, the method cannot be directly called with a parameter of a union type that matches the available overloads.

Here's a cut-down example that highlights the apparent issue (can be pasted into TypeScript playground):

    interface JQuery {
        append(content1: JQuery): JQuery;
        append(content1: string): JQuery;
    }

    var messageElement: JQuery;

    function setMessage(message: string | JQuery) {

        messageElement.append(message); // error JQuery is not assignable to type string

        // Help the compiler out with a type guard, it's okay
        if (typeof message === "string") {
            messageElement.append(message);
        } else {
            messageElement.append(message);
        }
    }

It's clear why the heavily-redundant type-guard workaround makes it easier for the compiler: in the general case each overload is free to return a different type, e.g. hypothetically weird example:

    interface JQuery {
        append(content1: JQuery): boolean;
        append(content1: string): number;
    }

This is an example of overloads capturing a specific relation that would be obscured if we instead used union types:

    interface JQuery {
        append(content1: JQuery|string): boolean|number;
    }

By writing it with union types, we don't capture the fact that JQuery goes to boolean, whereas string goes to number, but string never goes to boolean, etc..

But what I'm hoping is that compiler could behave as if there was an additional "catch all" overload that accepted the union of the parameters and return the union of the return types, i.e.:

    interface JQuery {
        append(content1: JQuery): boolean;
        append(content1: string): number;

                // Therefore the compiler infers the existence of this extra overload:
        append(content1: JQuery|string): boolean|number;
    }

That way, union type parameters would go into overloads more smoothly.

@nycdotnet
Copy link

@danielearwicker I also reported this in #1805 - can you respond to @danquirk 's question there?

@danquirk
Copy link
Member

danquirk commented Feb 2, 2015

Yeah, gonna dupe this and we can use that issue to discuss further.

@danquirk danquirk closed this as completed Feb 2, 2015
@danquirk danquirk added the Duplicate An existing issue was already created label Feb 2, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants