Skip to content

Latest commit

 

History

History
59 lines (41 loc) · 1.79 KB

no-base-to-string.md

File metadata and controls

59 lines (41 loc) · 1.79 KB

Requires that .toString() is only called on objects which provide useful information when stringified (no-base-to-string)

JavaScript will call toString() on an object when it is converted to a string, such as when + adding to a string or in ${} template literals.

The default Object .toString() returns "[object Object]", so this rule requires stringified objects define a more useful .toString() method.

Note that Function provides its own .toString() that returns the function's code. Functions are not flagged by this rule.

This rule has some overlap with restrict-plus-operands and restrict-template-expressions.

Rule Details

This rule prevents accidentally defaulting to the base Object .toString() method.

Examples of incorrect code for this rule:

// Passing an object or class instance to string concatenation:
'' + {};

class MyClass {}
const value = new MyClass();
value + '';

// Interpolation and manual .toString() calls too:
`Value: ${value}`;
({}.toString());

Examples of correct code for this rule:

// These types all have useful .toString()s
'Text' + true;
`Value: ${123}`;
`Arrays too: ${[1, 2, 3]}`;
(() => {}).toString();

// Defining a custom .toString class is considered acceptable
class CustomToString {
  toString() {
    return 'Hello, world!';
  }
}
`Value: ${new CustomToString()}`;

const literalWithToString = {
  toString: () => 'Hello, world!',
};

`Value: ${literalWithToString}`;

When Not To Use It

If you don't mind "[object Object]" in your strings, then you will not need this rule.