Jocket Jocket - 3 years ago 414
TypeScript Question

Is there a toString() version for typescript

Is there a

Javascript version for Typescript?
I have lot of objects and want to console print them. Some fields need not to be printed as well.
How can achieve this in Typescript?

Answer Source

No there isn't, however you can easily implement a property toString on your objects providing their "prettyfied" JSON representation:

toString = () => JSON.stringify(this, null, '  ');

Returning a selective JSON representation of the fields you need, can simply be achieved with Object.assign

 toString = () => JSON.stringify(Object.assign({}, {f1: this.f1, f2: this.f2}), null, '  ');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download