2

The following code:

class A {
    get p1(): number {
        return 1;
    }

    get p2(): number {
        return 2;
    }
}

const a = new A();

console.log(JSON.stringify(a));

Produces the following output:

{}

Is there a way to get something like the following?:

{
   "p1": 1,
   "p2": 2
}
Jesús López
  • 8,338
  • 7
  • 40
  • 66
  • You'd have to make those getters both instance-own and enumerable, like [this](https://tsplay.dev/WGAG9N), or customize the `toJSON` method like [this](https://tsplay.dev/wgQGMm). See the answers to the linked questions for more info – jcalz Oct 23 '22 at 21:56

0 Answers0