I have a decorator that simply does nothing:
export function myDecorator(target: any, key: string) {
var t = Reflect.getMetadata("design:type", target, key);
}
I use this decorator with a property of a class:
export class SomeClass {
@globalVariable
someProperty: string;
@globalVariable
fakeProperty: number;
}
Now, what I want to do is, get all the properties of the class decorated with the @globalVariable decorator.
I tried using "reflect-metadata" with:
Reflect.getMetadata('globalVariable', this);
but all I get is "undefined". Is this possible with reflect-metadata or am I getting this totally wrong?
Property decorators are called once per property definition within a class, when the class is defined.
This means that if you decorate the properties in SomeClass with @myDecorator:
export class SomeClass {
@myDecorator
someProperty: string;
}
Then the myDecorator function will be called with:
target: ( the SomeClass definition )
key : ( the name of the property )
When you enable metadata through the "emitDecoratorMetadata" property, the TypeScript compiler will generate the following metadata properties:
'design:type'
, 'design:paramtypes'
and 'design:returntype'
.
This then allows you to call Reflect.getMetadata with any of the above keys. i.e:
Reflect.getMetadata('design:type', ...)
Reflect.getMetadata('design:paramtypes',...)
Reflect.getMetadata('design:returntype', ...)
You cannot call Reflect.getMetadata with the name of the decorator.
What you need is to implement globalVariable in the following way:
function globalVariable(target: any, propertyKey: string | Symbol): void {
let variables = Reflect.getOwnMetadata("globalVariable", target.constructor) || [];
variables.push(propertyKey);
Reflect.defineMetadata("globalVariable", variables, target.constructor);
}
Then, on run time, you'll be able to call
Reflect.getMetadata('globalVariable', this);
as you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With