TS1238: Unable to resolve signature of class decorator when called as an expression

8.6k views Asked by At

I am seeing the following compilation error:

TS1238: Unable to resolve signature of class decorator when called as an expression.

Here is the code:

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());

Does anyone know how to fix that error?

1

There are 1 answers

0
Titian Cernicova-Dragomir On

The signature for a class decorator (as ou can find in lib.d.ts) has to be the following:

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;

So your class decorator can't have a field and desc parameter (or they should be optional if you plan to use the decorator as a field decorator as well)

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());