javascriptangulartypescriptangular-decorator

TypeScript decorator for setTimeout() function


I've started to learn how to implement TypeScript decorator in my application. So I started with setTimeout. It's an method decorator which executes method after some time.

For example:

@Decorators.timeout()
 public someMethod () {}

Here is my implementation:

export class Decorators {

  public static timeout (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): any {

    let originalMethod = descriptor.value;
    let decArguments = arguments;

    descriptor.value = function Timeout () {

        setTimeout(() => {

          originalMethod.apply(this, decArguments);

        }, 2000);
    };

    return descriptor;

  }

}

This is error I'm getting:

Supplied parameters do not match any signature of call target

What could be the problem?


Solution

  • You are missing args in your Timeout() function and you should pass those args to original method:

    descriptor.value = function Timeout (...args) {
      setTimeout(() => {
        originalMethod.apply(this, args);
      }, 2000);
    };
    

    You should then delete this line because it's not doing anything:

    let decArguments = arguments;