typescript/no-base-to-string Correctness
What it does
This rule requires toString() and toLocaleString() calls to only be called on objects which provide useful information when stringified.
Why is this bad?
JavaScript's toString() method returns '[object Object]' on plain objects, which is not useful information. This rule prevents toString() and toLocaleString() from being called on objects that return less useful strings.
Examples
Examples of incorrect code for this rule:
// These will evaluate to '[object Object]'
({}).toString();
({ foo: "bar" }).toString();
({ foo: "bar" }).toLocaleString();
// This will evaluate to 'Symbol()'
Symbol("foo").toString();Examples of correct code for this rule:
const someString = "Hello world";
someString.toString();
const someNumber = 42;
someNumber.toString();
const someBoolean = true;
someBoolean.toString();
class CustomToString {
toString() {
return "CustomToString";
}
}
new CustomToString().toString();Configuration
This rule accepts a configuration object with the following properties:
ignoredTypeNames
type: string[]
default: ["Error", "RegExp", "URL", "URLSearchParams"]
A list of type names to ignore when checking for unsafe toString usage. These types are considered safe to call toString on even if they don't provide a custom implementation.
How to use
To enable this rule using the config file or in the CLI, you can use:
{
"rules": {
"typescript/no-base-to-string": "error"
}
}oxlint --type-aware --deny typescript/no-base-to-string