Skip to content

Overloaded Union Return Types #3652

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

Closed
kitsonk opened this issue Jun 26, 2015 · 5 comments
Closed

Overloaded Union Return Types #3652

kitsonk opened this issue Jun 26, 2015 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@kitsonk
Copy link
Contributor

kitsonk commented Jun 26, 2015

Stupid question of the day, why does this not work:

class A {
    foo: string;
}

class B {
    bar: string;
}

class C {
    qat: string;
}

class D {
    baz: string;
}

function c(a: A): C;  /* Overloaded signature not compatible with function implementation */
function c(b: B): D;
function c(a: A|B): C|D {
    if (a instanceof A) {
        return new C();
    }
    else {
        return new D();
    }
}

Where as this is valid:

function c(a: A): C|D;
function c(b: B): C|D;
function c(a: A|B): C|D {
    if (a instanceof A) {
        return new C();
    }
    else {
        return new D();
    }
}

I am allowed to "delegate" the sorting out of the typing of my argument to my overloaded function implementation, but, I am not allowed to delegate the return type, though I am contracting to only return to return a certain type of return value (which is what I am doing with the handling arguments). Is there a good reason I am missing?

@nycdotnet
Copy link

It's a good question. See #1805. There's been talk of implementing a special case for this, but I haven't seen it yet.

@DanielRosenwasser
Copy link
Member

It's actually not quite related to that @nycdotnet.

It's actually in the spec. From 6.1 Function Declarations:

If a function declaration includes overloads, [...] the function implementation signature must be assignable to that type.

C | D isn't assignable to either C or D; in fact, it's the reverse (C and D are each assignable to C | D).

This leaves me wondering whether there's a better way to do this or if we need to reconsider this rule in the spec.

@kitsonk
Copy link
Contributor Author

kitsonk commented Jun 26, 2015

Hmm... That does feel reversed. Obviously the use case is that having determined which override is being invoked, the implementation is contracting to return that type, thereby not requiring further type disambiguqtion by the consumer.

@RyanCavanaugh
Copy link
Member

See #943

@kitsonk
Copy link
Contributor Author

kitsonk commented Jun 26, 2015

Yup, it is a duplicate of #943. I will take a look there.

@kitsonk kitsonk closed this as completed Jun 26, 2015
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 26, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

4 participants