reflect-metadata and querying the object for decorators
typescript
Solution
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.
Problem
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?