Here are two different decorators that I am using:
import "reflect-metadata";
const enum MetadataTypes {
Type = "design:type",
Paramtypes = "design:paramtypes",
ReturnType = "design:returntype"
}
function DecoratorA(target: any, key: string): void {
console.log(`Applied DecoratorA to ${key} on ${target.constructor.name}`);
const type = Reflect.getMetadata(MetadataTypes.Type, target, key);
console.log(type.name);
}
function DecoratorB(target: any, key: string): void {
console.log(`Applied DecoratorB to ${key} on ${target.name}`);
const type = Reflect.getMetadata(MetadataTypes.Type, target, key);
console.log(type);
}
I have applied one manually like this:
export class MyClass {
@DecoratorA
private foo: string;
}
And the other is applied using Reflect.decorate
:
Reflect.decorate([DecoratorB], MyClass, "foo");
I noticed that the decorator applied with Reflect does not retrieve the data type correctly. Why do you think this could be happening?
The output logs are as follows:
Applied DecoratorA to foo on MyClass
String
Applied DecoratorB to foo on MyClass
undefined