在TypeScript中检测通用类型 [英] Detecting generic type in TypeScript

查看:107
本文介绍了在TypeScript中检测通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的方法,该方法接受字符串作为参数,以从对象中查找为键.此方法具有通用类型,该类型将用于类型转换返回的对象.但是,这并没有达到预期的效果.真的可以打字输入值吗?如果可以,我该怎么做?

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?

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

    public getValue<T>(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<number>("test"); // Should return 1234
app.getValue<string>("test"); // Should return '1234'

推荐答案

我认为您在方法中混淆了keyT.我会这样写:

I think you are confusing key and T in your method. I would write it like that:

public getValue<T>(key : string) : T
{
    if (this.values.hasOwnProperty(key)) {
        switch (typeof key) {                  // 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!");
    }
}

通过使用游乐场,您将更好地理解TypeScript的工作原理.您可以看到您的代码如何编译:

You will get better understanding of how TypeScript works by using playground. You can see how your code compiles:

var Application = (function () {
function Application() {
    this.values = {
        "foo": "bar",
        "test": "1234"
    };
}
Application.prototype.getValue = function (key) {
    if (this.values.hasOwnProperty(key)) {
        switch (typeof T) {
            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!");
    }
};
return Application;
}());
var app = new Application();
app.getValue("test"); // Should return 1234
app.getValue("test"); // Should return '1234'

已编译的JS中没有T.它只会在您预先编译的TypeScript中可见.

There is no T in compiled JS. It will be only visible in your TypeScript, pre compiled.

除此之外,您将无法拨打电话:

Apart from that, you are not able to call:

getValue<VALUE>(...)

这篇关于在TypeScript中检测通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆