Closed
Description
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