Skip to content

Generics use last rather than first matching overload #20257

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
SamPruden opened this issue Nov 25, 2017 · 2 comments
Closed

Generics use last rather than first matching overload #20257

SamPruden opened this issue Nov 25, 2017 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@SamPruden
Copy link

TypeScript Version: 2.7.0-dev.20171124

Code

class Foo {
    bar(): 1;
    bar(): 2;
    bar(): number {
        return 1;
    }
}

interface WithBar<R> {
    bar(): R;
}

function test<R>(a: WithBar<R>): R {
    return a.bar();
}

const foo = new Foo();
const n1 = foo.bar(); // Has type 1
const n2 = test(foo); // Has type 2

Expected behavior:
n1 and n2 have the same type, namely 1.

Actual behavior:
n2 gets the incorrect type 2. It seems that R is taken from the last matching overload, rather than the first.

I don't know if there's some detailed spec doc somewhere that I could use to confirm that this is a bug rather than a weird design, but it's definitely undesirable to me.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 27, 2017

The assumption here is that the last signature is more general, and thus leads to better inferences.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 27, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants