从方法装饰器获取方法签名

Get signature of method from method decorator

我有一个这样的方法装饰器:

export function NumberMethodDecorator(message: string) {
  return (target: object, propertyKey: string, descriptor?: TypedPropertyDescriptor<any>) => {
    // do some stuff here
  }
}

我想这样应用:

class SomeClass {

  @NumberMethodDecorator("do some cool stuff")
  public someMethod(value: number) {

  }

}

但是,我想确保 NumberMethodDecorator 仅应用于签名为 (value: number) => any 的方法。

我该怎么做?

TypedPropertyDescriptor 的类型参数中指定:

export function NumberMethodDecorator(message: string) {
  return (
    target: object, propertyKey: string,
    descriptor?: TypedPropertyDescriptor<(value: number) => any>
  ) => {
    // do some stuff here
  };
}

然后使用时:

class SomeClass {
  @NumberMethodDecorator("") // ok
  someMethod(value: number) {

  }

  @NumberMethodDecorator("") // compile error
  otherMethod() {
  }
}