打字稿:装饰构造函数公共参数?

时间:2016-07-23 16:33:10

标签: typescript decorator

有没有办法使用构造函数参数public简写来装饰成员?

如果我这样做:

class Test{
  constructor(
    @decorator public b
  ){ }
}

然后,这是被装饰的参数,而不是成员。这是编译结果:

var Test = function () {
    function Test(b) {
        this.b = b;
    }
    Test = __decorate([__param(0, decorator)], Test);
    return Test;
}();

因此,我发现使其按预期工作的唯一方法是:

class Test{
  @decorator b;
  constructor(b){
    this.b = b;
  }
}

产生:

var Test = function () {
    function Test(b) {
        this.b = b;
    }
    __decorate([decorator], Test.prototype, 'b');
    return Test;
}();

我知道它并不是那么糟糕但重复4次相同的变量名称会让我感到恶心。

1 个答案:

答案 0 :(得分:1)

  

有没有办法使用构造函数参数public shorthand

来装饰成员

没有。

相关问题