简体   繁体   English

Typescript 对象类型比较

[英]Typescript object type comparison

I'm trying to compare object types in typescript.我正在尝试比较打字稿中的对象类型。

Is there a way to pass a type as a parameter and compare?有没有办法将类型作为参数传递并进行比较? something like the following C# code in typescript?类似于打字稿中的以下 C# 代码?

class A
{
}
class B
{
}
void Main()
{
    TypeTest(typeof(B));
}
void TypeTest(Type t)
{
    if (t == typeof(A))
    {
        ...
    }
    else (t == typeof(B))
    {
        ...
    }
}

Typescript types are erased at runtime, however maybe you could make do with a perfectly vanilla javascript feature: prototypes. Typescript 类型在运行时会被删除,但是也许您可以使用一个完美的原生 JavaScript 功能:原型。

class A {}
class B {}

function typeTest(prototype: any) {
    if (prototype === A.prototype) {
        //
    } else if (prototype === B.prototype) {
        //
    }
}

typeTest(A.prototype)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM