Require .toString() to only be called on objects which provide useful information when stringified.
// code-linter.json5
{
"rules": {
"@typescript-eslint/no-base-to-string": "error"
}
} // These types all have useful .toString()s
const num = 123;
export const v1 = 'Text' + true;
export const v2 = `Value: ${num}`;
(() => {
console.info('arrow function');
}).toString(); interface MyType {
name: string;
}
// Passing an object or class instance to string concatenation:
const obj: MyType = {
name: 'object'
};
export const v1 = '' + obj;
class MyClass {}
const value = new MyClass();
export const v2 = value + '';
// Interpolation and manual .toString() calls too:
export const v3 = `Value: ${value}`;
export const v4 = obj.toString(); plugin:@typescript-eslint/all
For details about how to configure Code Linter check rules, see Code Linter.