如何在打字稿中读取实例装饰器?

时间:2021-07-22 21:20:13

标签: javascript typescript decorator reflect-metadata

我可以使用 reflect-metadata 创建自定义装饰器并且工作正常。

问题是,我不知道如何获取所有实例装饰器。

import 'reflect-metadata';

console.clear();

function readTypes() {
  const decorator: MethodDecorator = (target, propertyKey, description) => {
    const args = Reflect.getMetadata(
      'design:paramtypes',
      target,
      propertyKey
    ).map(c => c.name);
    const ret = Reflect.getMetadata('design:returntype', target, propertyKey);
    console.log(`Arguments type: ${args.join(', ')}.`);
    console.log(`Return type:    ${ret.name}.`);
  };
  return decorator;
}

class Foo {}

class Bar {
  @readTypes()
  public fn(a: number, b: string, c: Foo): boolean {
    return true;
  }
}

const barInstance = new Bar();

我想从 @readTypes 获取所有带有装饰器 barInstance 的函数。我该怎么做?

参见工作示例: https://stackblitz.com/edit/decorators-metadata-example-nakg4c

1 个答案:

答案 0 :(得分:0)

首先,您没有编写任何元数据,只是读取它。如果要查找哪些属性被修饰,则必须将元数据写入这些属性。

为了简单起见,让我们将装饰器简化为:

// It's a best practice to use symbol as metadata keys.
const isReadTypesProp = Symbol('isReadTypesProp')

function readTypes() {
  const decorator: MethodDecorator = (target, propertyKey, description) => {
    Reflect.defineMetadata(isReadTypesProp, true, target, propertyKey);
  };
  return decorator;
}

现在当使用装饰器时,它会在解析类时执行,而不是在创建实例时执行。这意味着装饰器函数中的target实际上是类构造函数的prototype

换句话说,target 是同一个对象 Bar.prototypebarInstance.constructor.prototypebarInstance.__proto__

知道我们可以遍历原型对象中的所有属性名称并查找我们之前设置的元数据:

function getReadTypesPropsFromInstance(object: {
  constructor: {
    prototype: unknown;
  };
}) {
  const target = object.constructor.prototype;
  const keys = Object.getOwnPropertyNames(target);
  return keys.filter(key => Reflect.getMetadata(isReadTypesProp, target, key));
}

现在返回修饰属性的名称:

const barInstance = new Bar();
console.log(getReadTypesPropsFromInstance(barInstance)); // ["fn"]

enter image description here

相关问题