Possibillity从clas创建装饰器

时间:2015-06-02 18:37:42

标签: typescript

我想知道是否可以使用TypeScript 1.5从类创建属性装饰器?我的意思是从

切换
 function Inject(value = null) {
    return function (target:Object, propertyKey:string) {
        target[propertyKey] = value;
    };
}

到类

class Inject{...} 

如果有可能我们该怎么做?

感谢您提前

2 个答案:

答案 0 :(得分:2)

来自TypeScript wiki(以及装饰提案):

装饰者是:

  • 表达
  • 评估函数
  • 将目标,名称和属性描述符作为参数
  • 并可选择返回要在目标对象上安装的属性描述符

所以,不。但是,您可以使用您想要的任何类型的代码实现装饰器的逻辑,其中包括封装在类中的逻辑。

答案 1 :(得分:0)

在类中创建装饰器的示例:

ArrayList<String> myList = ...
// Notice diamond
HashSet<String> mySet = new HashSet<>( myList );
// Really no reason now to use Guava for HashSet construction in most cases

如果您只想要一种将装饰器组合在一起的方法,这可能会有所帮助。