Is there a solution for avoiding the TypeScript error when trying to create a shared utility function for dumping scattered objects in code without repeating the same code over and over? I have tried different approaches and it seems to work fine in Chrome browser JS.
function formatthis() { return JSON.stringify(this); }
...
function whatever() {
...
var something = { aa:234, bb:123, toString:formatthis };
...
var something2 = { dfwerg:22, wer:11, toString:formatthis };
...
var something3 = { sergw:55, qsfds:33, toString:formatthis };
...
console.log('looks like '+something);
...
}
UPDATE: The example has been updated to highlight the reusability of methods among different objects. While it works globally in Chrome browser, including functions and with "use strict", // @ts-ignore may compile but does not consistently work in Node environments, for reasons that are unclear.