I think I've run into a bug in TypeScript's function overload resolution. Here's an example:
class Container<T>
{
constructor(public value: T) { }
}
function unwrap<T>(value: Container<T>): T;
function unwrap<T>(value: T): T;
function unwrap(obj) {
if (obj instanceof Container) {
return obj.value;
} else {
return obj;
}
}
// Inferred type of "a" should be number, but is in fact Container<number>
var a = unwrap(new Container<number>(1));
var b = unwrap(2);
var c = a.toFixed(); // causes compile error
By my understanding of the overload resolution rules in section 4.12.1 of the TypeScript language spec, the statement assigning to "a" should use the first unwrap overload, and the assignment to "b" should use the second overload. So both "a" and "b" should have inferred type "number". However in fact the compiler seems to use the second unwrap overload for both calls, so the inferred type of "a" is Container<number>, and therefore the assignment to "c" fails to compile. Is this a bug, or have I misunderstood the overload resolution rules?
I'm still using TypeScript 0.9.0.1 by the way, because my project requires it for compatibility, so maybe this is already fixed in a newer version?