【发布时间】:2015-10-23 19:50:30
【问题描述】:
bar 是一个简单的类装饰器,它为类 Foo 添加了一个属性。
function bar(target) {
target.inDecorator = 'in decorator';
}
@bar
class Foo {
inClass:string;
inDecorator:string;
constructor() {
this.inClass = 'a string';
}
getInClass() {
return this.inClass;
}
}
console.log(Foo.inDecorator);
console.log(Foo.prototype.inDecorator);
const foo = new Foo();
console.log(foo.getInClass());
console.log(foo.inDecorator);
唯一导致错误的控制台日志是第一个,Foo.inDecorator,在 ts 1.5.3 中包含它
Property 'inDecorator' does not exist on type 'typeof Foo'.
据我所知,inDecorator 应该定义在 Class Foo 的原型上,并且应该在 Foo 上可用,就好像它是一个静态道具一样。运行生成的 js 文件显示原型访问以及新 foo 对象上的未定义,但是 Foo.inDecorator 正确打印,即使它是错误的来源。为了更清楚,我们得到
in decorator
undefined
a string
undefined
关于如何正确输入/添加静态道具或方法的任何想法?
谢谢!
编辑这个,因为我最初忽略了原型访问,Foo.prototype.inDecorator 不起作用的事实。
【问题讨论】:
标签: javascript class typescript decorator typeof