Skip to content

Type inference issue with overload signatures and string literal types #15086

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
marcomura opened this issue Apr 8, 2017 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@marcomura
Copy link

marcomura commented Apr 8, 2017

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

interface Base {
    key: string;
}
interface A extends Base {
}
interface B extends Base {
}

export function test(key: "aa"): A;
export function test(key: "bb1"): B;
export function test(key: "bb2"): B;

export function test(key: string): Base {
    return { key: key };
}

export function bbTest(key: "bb1" | "bb2"): B {
    return test(key);
}

playground url:
here

Expected behavior:
No compiler errors; type inference to understand that "return test(key)" returns a B type.

Actual behavior:
"return test(key)" doesn't compile because of an argument type error

@aluanhaddad
Copy link
Contributor

This is working correctly since the overloads only differ by the type of one argument but have the same return type.

You should rather write

export function test(key: "aa"): A;
export function test(key: "bb2" | "bb1"): B;
export function test(key: string): Base {
    return { key: key };
}

See here https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html for more information (end of page)

@marcomura
Copy link
Author

I'm sorry @aluanhaddad, but no, it's not working correctly.
It doesn't make sense to define the test() overloads as you described, because I could have way more definitions and exponentials permutations.

E.g.

export function test(key: "bb1"): B;
export function test(key: "bb2"): B;
export function test(key: "bb3"): B;
export function test(key: "bb4"): B;

export function testB1(key: "aa1" | "aa2"): B { return test(key); }
export function testB2(key: "aa1" | "aa3"): B { return test(key); }
export function testB3(key: "aa1" | "aa2" | "aa3"): B { return test(key); }
export function testB4(key: "aa2" | "aa3" | "aa4"): B { return test(key); }
export function testB5(key: "aa1" | "aa2" | "aa3" | "aa4"): B { return test(key); }
// etc ...

Typescript has all the information at compile time to understand that calling test() with an argument of type "bb1" | "bb2" will surely produce a return of type B, so it's definitely an issue that it's not working right now.

Thanks

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Feb 8, 2018

Duplicate of #14107

@mhegazy mhegazy marked this as a duplicate of #14107 Feb 8, 2018
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 8, 2018
@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 Jul 3, 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

5 participants