使用装饰器获取已实现接口的列表 [英] Using a Decorator to get list of implemented interfaces

查看:92
本文介绍了使用装饰器获取已实现接口的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道是否有可能使用装饰器获得由类实现的接口数组:

Do you know if it is possible to get the array of interfaces implemented by a class using a decorator:

interface IWarrior {
  // ...
}

interface INinja {
  // ...
}

所以,如果我做类似的事情:

So If I do something like:

@somedecorator
class Ninja implements INinja, IWarrior {
 // ...
}

在运行时,Ninja的注释将包含["INinja", "IWarrior"]?

At run-time Ninja will have an annotation which contains ["INinja", "IWarrior"] ?

谢谢

推荐答案

当前,类型仅在开发和编译期间使用.类型信息不会以任何方式转换为已编译的JavaScript代码.但是,您可以像这样将字符串列表传递给decorator参数:

Currently, types are used only during development and compile time. The type information is not translated in any way to the compiled JavaScript code. But you however can pass the list of strings to the decorator parameter like this:

interface IWarrior {
  // ...
}

interface INinja {
  // ...
}


interface Function {
    interfacesList: string[];
}

@interfaces(["INinja", "IWarrior"])
class Ninja implements INinja, IWarrior {

}

function interfaces(list: string[]) {
    return (target: any) => {
        target.interfacesList = list; 
        return target;
    }
}

console.log(Ninja.interfacesList);

这篇关于使用装饰器获取已实现接口的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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