TypeScript has a built in feature for defining accessor properties
class Test {
constructor(private value: number = 123) {
}
public get Value(): number {
return this.value;
}
public set Value(value: number) {
this.value = value;
}
}
Compiler output
var Test = (function () {
function Test(value) {
if (value === void 0) { value = 123; }
this.value = value;
}
Object.defineProperty(Test.prototype, "Value", {
get: function () {
return this.value;
},
set: function (value) {
this.value = value;
},
enumerable: true,
configurable: true
});
return Test;
})();
JavaScript also supports value properties
Object.defineProperty(someObj, "MyValueProperty", {
// The key here is value as opposed to get and set.
value: 5
enumerable: true,
configurable: false
});
How do you define value propertes with TypeScript?
NOTICE: I notice that I'm being pointed to another stackoverflow question regarding TypeScript getters and setters. This is not what I want. I want to know how to create properties that implement value, not get and set!