Skip to content
← Back to rules

typescript/no-base-to-string Correctness

✅ This rule is turned on by default when type-aware linting is enabled.
💭 This rule requires type information.

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:

ts
// 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:

ts
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:

checkUnknown ​

type: boolean

default: false

Whether to also check values of type unknown. When true, calling toString on unknown values will be flagged. Default is false.

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:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-base-to-string": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-base-to-string": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-base-to-string

Version ​

This rule was added in v1.12.0.

References ​