Here is an example code snippet:
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
It may be possible to make the _foobar
variable public and rename it to foobar
in order to avoid creating new property names. However, attempting to modify that property externally should not result in a compile-time error. Instead, it should cause a run-time error when the property is modified either internally or externally.