[英]TypeScript: Is it possible to define new class members using decorators?
例:
function Getter(target: any, key: string): void {
let getter = () => this[key];
/* create "foobar" property from "_foobar" */
Object.defineProperty(target, removeUnderscores(key), {
get: getter,
enumerable: true,
configurable: true
});
}
class Foo {
@Getter
private _foobar: string;
constructor() {
this._foobar = "Hello World" // OK
console.log(this.foobar) // Compiler error
}
}
let foo = new Foo();
console.log(foo.foobar) // Compiler error
我想可以将_foobar
公开并将其重命名为foobar
以避免创建新的属性名称,但是尝试从外部修改该属性不会导致编译时错误,它会在内部或外部修改时导致运行时错误。
console.log(this.foobar)//编译器错误
您无法根据TypeScript装饰器基础结构的当前语义删除此编译器错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.