Typescript泛型是否使用类型删除来实现泛型? [英] Do Typescript generics use type erasure to implement generics?

查看:273
本文介绍了Typescript泛型是否使用类型删除来实现泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类助手
{
static ObjectAs< T> ;(val:any):T {
if(!(val instanceof T)){
return null;
}

return< T> val;


$ / code>

获取泛型声明基础类型的任何解决方法

Update 1:

正如Ryan Cavanaugh所说,整个系统被删除,更像Java的泛型实现。我相信.Generics的Net实现在编译代码中保留类型信息比Java更好。
但是,我不禁想知道,为什么不能在TypeScript中允许对泛型类型和泛型类型参数进行完整的运行时自检?为什么TypeScript的设计师决定在运行时删除所有泛型类型信息?



以下TypeScript代码:

<$ p $函数As< T>(n:any):T {
if(n instanceof T){
return n;
} else {
return null;
}
}

var obj:Object = 1;
var x = As< number>(obj);

可以转换成以下JavaScript代码:

  function As(n,T){
if(n instanceof T){
return n;
} else {
return null;
}
}

var obj = 1;
var x = As(obj,Number);

在运行时保存类型信息并与js代码兼容!



更新2:
我在CodePlex上发布了这个问题,希望能从TypeScript中获得更多信息
https://typescript.codeplex.com/discussions/550262

解决方案

类型系统被完全删除。你可以在任何泛型函数的生成代码中看到它。



就是说,对于类,还是有一些运行时信息。你可以这样写:

  class A {} 
class B {}
function As< T> (n:any,type:{new(... args:any []):T}):T {
if(n instanceof type){
return n;
} else {
return null;
}
}

var x = new A();
var y = new B();
console.log(As(x,A));
console.log(As(x,B));


Meaning you cannot use something like this?

class Helpers
{
    static ObjectAs<T>(val: any): T {
        if (!(val instanceof T)) {
            return null;
        }

        return <T>val;
    }
}

Any workaround to get the underlying type of the generic declaration?

Update 1:

As Ryan Cavanaugh mentioned, when compiled, the whole type system is erased, more like Java's implementation of Generics. I believe .Net implementation of Generics to preserve type information in the compiled code is better than Java. However, I cannot help but wonder, why is not possible to permit full runtime introspection of generic types and generic type parameters in TypeScript? Why designers of TypeScript decided to remove all generic type information at runtime?

The following TypeScript code:

function As<T>(n: any): T {
    if (n instanceof T) {
        return n;
    } else {
        return null;
    }
}

var obj: Object = 1;
var x = As<number>(obj);

Could be translated to this JavaScript code:

function As(n, T) {
    if (n instanceof T) {
        return n;
    } else {
        return null;
    }
}

var obj = 1;
var x = As(obj, Number);

Preserving the type information at runtime and compatibility with the js code!

Update 2: I've posted the issue on CodePlex, hoping to get more from TypeScript people https://typescript.codeplex.com/discussions/550262

解决方案

The type system is wholly erased. You can see this in the generated code for any generic function.

That said, for classes, there's still some runtime information left. You could write this:

class A { }
class B { }
function As<T>(n: any, type: { new(...args: any[]): T }): T {
    if(n instanceof type) {
        return n;
    } else {
        return null;
    }
}

var x = new A();
var y = new B();
console.log(As(x, A));
console.log(As(x, B));

这篇关于Typescript泛型是否使用类型删除来实现泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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