Skip to content

Incorrect type inference when passing union type to a method or indexer #7274

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
DavidKDeutsch opened this issue Feb 27, 2016 · 1 comment
Closed
Labels
Bug A bug in TypeScript Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Duplicate An existing issue was already created

Comments

@DavidKDeutsch
Copy link

Not sure if this is a bug or just a feature that has not been implemented (yet), but given the following:

interface Foo {
    member1: number;
    member2: boolean;
}

function bar(foo: Foo, member: "member1" | "member2") {
    return foo[member]; // return type is inferred as any, whereas it should be number | boolean
}

I would expect the return type of Foo to be number | boolean instead of any. I see the same issue with function overloads:

declare function bar2(field: "field1"): string;
declare function bar2(field: "field2"): number;
declare function bar2(field: string): any;

var field: "field1" | "field2" = "field1";
var result = bar2(field); // inferred type of result is any, whereas it should be string | number

Note that his behavior is not limited to string literals; it also happens when overloading on type:

declare function bar3(field: Date): string;
declare function bar3(field: number): number;
declare function bar3(field: any): any;

var field2: Date | number = 6;
var result2 = bar3(field2); // inferred type of result2 is any, whereas it should be string | number

This is with typescript 1.8.2

@DanielRosenwasser DanielRosenwasser added Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Bug A bug in TypeScript labels Mar 3, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2016

The first sample is a duplicate of #7730

The last two samples are by design. It becomes easier to see why this is not possible for the general case once you have more than one parameter.

declare function bar2(field: "field1", a: number): string;
declare function bar2(field: "field2", b: string): number;

var result = bar2(field, "some string"); // number, or string, or number|string, or error?

This is already suggested in #1805, and you can find more discussions about these at #6735

@mhegazy mhegazy closed this as completed Apr 26, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 26, 2016
@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
Bug A bug in TypeScript Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants