TS1240

Unable to resolve signature of property decorator when called as an expression. The runtime will invoke the decorator with 2 arguments, but the decorator expects 3.

Broken Code ❌

function CatchError() {
  return function (target: any, _propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
    function replacementMethod(this: any, ...args: any[]) {
      try {
        return target.apply(this, ...args);
      } catch (error) {
        console.error('Yikes', error);
      }
    }
 
    return replacementMethod();
  };
}
 
class Example {
  @CatchError()
  function doSomething() {
    throw new Error('I am failing!');
  }
}
 
const example = new Example();
example.doSomething();

The problem is that the CatchError decorator is defined as a method decorator but is being used as a property decorator.

Fixed Code ✔️

To fix this issue, turn the method into a property by removing the function keyword:

function CatchError() {
  return function (target: any, _propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
    function replacementMethod(this: any, ...args: any[]) {
      try {
        return target.apply(this, ...args);
      } catch (error) {
        console.error('Yikes', error);
      }
    }
 
    return replacementMethod();
  };
}
 
class Example {
  @CatchError()
  doSomething() {
    throw new Error('I am failing!');
  }
}
 
const example = new Example();
example.doSomething();