In my TypeScript code, I am utilizing getter/setter accessors. To differentiate between variables and methods with the same name, I have adopted the convention of prefixing the variable with a lower dash, as shown in many examples:
private _major: number;
get major(): number {
return this._major;
}
set major(major: number) {
this._major = major;
}
When I use JSON.stringify() to convert the object into a JSON string, it uses the variable name (_major) as the key.
I prefer not to have all keys prefixed with a lower dash in the JSON output file. Is there a way for TypeScript to utilize the name of the getter method as the key instead? Or are there other techniques to maintain clean JSON output while still using getter/setter methods?
Although I am aware of manual approaches to modify JSON keys before generating the output string, I am interested in finding a simpler solution.
Here is a JSFiddle that showcases the current behavior.