【问题标题】:Decorators in typescript throws compilation error打字稿中的装饰器引发编译错误
【发布时间】:2020-01-30 01:16:54
【问题描述】:

我正在尝试在 typescript 中测试装饰器,但是当我编译 ts 代码时出现错误

error TS1241: Unable to resolve signature of method decorator when called as an expression.

error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.

我在 tsconfig.json 中有正确的装饰器配置

{
  "compilerOptions": {
      "target"                  : "ES5",
      "experimentalDecorators"  : true,
      "emitDecoratorMetadata"   : true
  }
}

我已经尝试了在互联网上找到的所有方法来解决这个问题,但错误没有发生

tsc 版本 3.4.2

根据一篇帖子的问题是因为在调用装饰器时编译os ts代码,它是用少一个参数调用的

__decorate([
        f()
    ], C.prototype, "ffolow");

使用装饰器的类是

class C {

  @f()
  ffolow() {
    console.log("FF called")
  }

}

【问题讨论】:

标签: typescript decorator


【解决方案1】:

这取决于你的装饰器是如何定义的。假设它只是一个“常规”装饰器(不是装饰器工厂),那么你应该不带括号应用它:

function f(target: any, propertyName: string, descriptor: PropertyDescriptor) {
  //
}

class C {

  @f
  ffolow() {
    console.log("FF called")
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2018-02-12
    • 2020-12-24
    • 2018-06-21
    • 2019-07-29
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多