An object that uses the default Object.prototype.toString() becomes "[object Object]" when converted to a string.
Messages, logs, and generated text then show that placeholder instead of the data the code meant to include. Convert the value you want to show,
usually one of the object’s properties.
This rule reports such objects in template literals, ` and `= concatenations, String(), toString() and
toLocaleString() calls, Lodash _.toString(), and join() on arrays of them.
The template literal converts the whole user object:
const user = { name: "Ada", id: 42 };
console.log(`Signed in: ${user}`); // Noncompliant: prints "Signed in: [object Object]"
Interpolate the property the message needs:
const user = { name: "Ada", id: 42 };
console.log(`Signed in: ${user.name}`);
Define a toString() method instead when a class has one text form that every caller should get. To log the whole object, pass it as a
separate argument, as in console.log("Signed in:", user), or use JSON.stringify(user). JSON.stringify throws on
circular references and BigInt values, and omits functions and undefined properties.
The rule does not report a conversion that the surrounding code already guards. Any one of these guards is enough:
toString() call is the first statement of a branch that compares the object’s toString with
Object.prototype.toString.toString() call is stored in a constant and compared with "[object Object]" before it is
used.typeof checks show that the value cannot be an object.Error, RegExp, URL, and URLSearchParams values are not reported.
Object.prototype.toString()