I am using getter / setter assemblers in TypeScript. Since it is impossible to have the same name for a variable and a method, I started the variable prefix with a lower dash, as was done in many examples:
private _major: number; get major(): number { return this._major; } set major(major: number) { this._major = major; }
Now, when I use the JSON.stringify () method to convert the object to a JSON string, it will use the variable name as the key: _major.
As I donβt want the JSON file to have all the keys prefixed with a lower dash, is it possible to make TypeScript use the getter method name, if available? Or are there other ways to use getter / setter methods, but still produce pure JSON output?
I know that there are ways to manually modify the JSON keys before they are written to the output of the string. I am curious if there is a simpler solution.
Here is a JSFiddle that demonstrates the current behavior.
source share