Detecting generic type in TypeScript
<p>I'm writing a simple method which accepts a string as an argument to look up as a key from an object. This method has a generic type, which will be used to typecast the returned object. However, this isn't quite working as expected. Is it actually possible to typecast values, and if so, how do I do this?</p>

<pre><code>class Application
{
    private values : {[s : string] : string} = {
        "foo" : "bar",
        "test" : "1234"
    }

    public getValue&lt;T&gt;(key : string) : T
    {
        if (this.values.hasOwnProperty(key)) {
            switch (typeof T) {                  // Doesn't work
                case "string":
                    return this.values[key].toString();
                case "number":
                    return parseInt(this.values[key]);
                default:
                    throw new Error("Type of T is not a valid return type!");
            }
        } else {
            throw new Error("Key '" + key + "' does not exist!");
        }
    }
}

var app : Application = new Application();
app.getValue&lt;number&gt;("test"); // Should return 1234
app.getValue&lt;string&gt;("test"); // Should return '1234'
</code></pre>