TS1238:作为表达式调用时无法解析类装饰器的签名

Ale*_*lls 5 typescript tsc typescript3.0

我看到以下编译错误:

TS1238:作为表达式调用时无法解析类装饰器的签名。

这是代码:

const fdec = function(target:any, field: any, desc: any){
  console.log('target 0 :', target);
  target.bar = 3;
  return target;
};

const fdec2 = function(){
  console.log('target 1:');
  return function(target:any, field: any, desc: any){
    console.log('target 2:', target);
    target.bar = 3;
    return target;
  }
};

@fdec
@fdec2()
class Foo {
  static bar: number
}


console.log(Foo.bar);
console.log(new Foo());
Run Code Online (Sandbox Code Playgroud)

有谁知道如何修复该错误?

Tit*_*mir 5

类装饰器的签名(可以在 lib.d.ts 中找到)必须如下:

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
Run Code Online (Sandbox Code Playgroud)

所以你的类装饰器不能有field anddesc参数(或者如果你计划使用装饰器作为字段装饰器,它们应该是可选的)

const fdec = function (target: any) {
    console.log('target 0 :', target);
    target.bar = 3;
    return target;
};

const fdec2 = function () {
    console.log('target 1:');
    return function (target: any) {
        console.log('target 2:', target);
        target.bar = 3;
        return target;
    }
};

@fdec
@fdec2()
class Foo {
    static bar: number
}


console.log(Foo.bar);
console.log(new Foo());
Run Code Online (Sandbox Code Playgroud)