The following code Sample is working as expected and prints out "[Function: Date]"
import 'reflect-metadata'
function logType(target : any, key : string) {
var t = Reflect.getMetadata("design:type", target, key);
console.log(`${key} type: ${t.name}`);
}
export class Demo {
@logType // apply property decorator
test: Date;
}
let demo = new Demo();
console.log(Reflect.getMetadata('design:type', demo, "test"));
If I place the same code within an Angular 2 Project, "function Object() { [native code] }" is returned.
I prepared a Plunker for this: https://plnkr.co/edit/DhXT89U0q5fCOWlCrx6w?p=preview
Reflect.getMetadata('design:type' ...) is still working for custom classes and other builtin classes. I could only produce this problem with Date.
What am I doing wrong?
You have to execute the function, and not just.. put it there :), you should add parentheses after
@logType
On the other hand you should change your
@logType
function to something like this:Which you can call like this:
I've updated the plnkr to show what I mean: plnkr
You can only get either
string
,boolean
,number
orobject
with the built-in types. Whereobject
will be anything, fromDate
toArray
to yourMath
andWeakMap
. All built-ins will evaluate toObject
, I'm not sure if this a bug, or by design.You can however use your method to get custom classes. If you would do
It will print out
Demo