我想为属性创建一个装饰,允许我自动添加属性而无需将其写入构造函数。我对 Java/Type 脚本还很陌生,所以希望我没有把这个杀得太糟糕。我似乎无法做到这一点,这是我目前所拥有的......
目标是通过以下方式:
class A {
@uuid
'property': string;
'another': string;
}
function uuid(target: any, key: string): void {
Reflect.defineMetadata('isUuid', true, target, key);
}
我以后可以使用A
's 构造函数new () => Object
来获取所有属性的列表以及它们是否是 UUID。我认为这看起来像:
Object.keys(A).forEach(key => {
console.log(`[${key}].isUuid? ${Reflect.getMetadata('isUuid', A, key) === true}`);
});
这有望产生类似的东西:
[property].isUuid? true
[another].isUuid? false
作为说明,如果我将课程更改A
为:
class A {
constructor() {
this.property = undefined;
this.another = undefined;
}
@uuid
'property': string;
'another': string;
}
我可以让它工作,但我必须创建一个实例A
才能获取密钥并获取它们的元数据。