jkchao / typescript-book-chinese

TypeScript Deep Dive 中文版
https://jkchao.github.io/typescript-book-chinese/
MIT License
6.54k stars 674 forks source link

Reflect Metadata demo有错 #252

Open ssxsite opened 2 years ago

ssxsite commented 2 years ago

(这篇文章的demo有问题:https://jkchao.github.io/typescript-book-chinese/tips/metadata.html#%E5%9F%BA%E7%A1%80))。 原文: 譬如在 vue-property-decorator 6.1 及其以下版本中,通过使用 Reflect.getMetadata API,Prop Decorator 能获取属性类型传至 Vue,简要代码如下:

function Prop(): PropertyDecorator { return (target, key: string) => { const type = Reflect.getMetadata('design:type', target, key); console.log(${key} type: ${type.name}`); // other... }; }

class SomeClass { @Prop() public Aprop!: string; } `

代码运行有问题。改成如下则没问题:

function Prop(): Function { return (target, key: string) => { const type = Reflect.getMetadata('design:type', target, key); console.log(${key} type: ${type.name}`); // other... }; }

class SomeClass { @Prop() public Aprop!: string; } `