如何从打字稿注释中获取 class 名称

How to get the class name from a typescript annotation

我在方法级别使用打字稿注释。我希望能够从注释中获取 class 或文件名。

const some = (arg: string) => {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    const originalMethod = descriptor.value;
    descriptor.value = function (...args: any[]) {
      // get class name here <------------------------
      const result = originalMethod.apply(this, args);
      return result;
    };
  };
};


class Foo(){

  @some("xyz")  
  bar(){
    // do something
  }
}

有什么想法吗?

对于 instance member,使用 class 的 原型调用装饰器。

function some(arg: string) {
  return (targetPrototype: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    const originalMethod = descriptor.value;
    const className = targetPrototype.constructor.name;
    descriptor.value = function (...args: any[]) {
        console.log(className);
    };
  };
};