我想创建一个属性的装饰,这将允许我自动添加属性,而无需将其写入构造函数。我对Java/Type脚本相当陌生,所以希望我没有把这个写得太糟糕。我似乎无法做到这一点,这是我迄今为止...Typescript属性修饰器自动添加到原型
的目标是,用线沿线的东西:
class A {
@uuid
'property': string;
'another': string;
}
function uuid(target: any, key: string): void {
Reflect.defineMetadata('isUuid', true, target, key);
}
我可以在以后使用A
“的构造函数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
的实例,以便能够获取密钥并获取这些元数据。
为什么你需要非uuid属性? – jcalz
我想要做的是使用装饰器为该属性添加额外的条件。这最终将用作数据库表的模型。 – TheHebrewHammer