# @typescript-eslint/no-base-to-string

> Disallow turning an object into a string when the result is `[object Object]`

An object that doesn't define its own `toString()` turns into the text `"[object Object]"`. That text ends up in a log line, a URL or a message on the page, and the data you wanted to show is lost.

The rule checks `.toString()`, `.toLocaleString()`, `String(value)`, `"text" + value`, `${value}` in a template, and `.join()` on an array of such objects. The report says "will" if the type always gives `[object Object]`, and "may" if only some members of a union do.

`bun lint --type-aware` turns this rule on in TypeScript files.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. This rule needs types, so turning it on makes `bun lint` type check your code first.

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/no-base-to-string" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/no-base-to-string" = ["error", {
  ignoredTypeNames = ["Error", "RegExp", "URL", "URLSearchParams"],
  checkUnknown = false,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```ts
const user = { name: "Ada" };

console.log(`Hello, ${user}`);
console.log("Hello, " + user);
console.log([user].join(", "));
```

Not reported:

{/* good */}
```ts
const user = { name: "Ada" };

console.log(`Hello, ${user.name}`);
console.log(JSON.stringify(user));

class Point {
  constructor(private x: number, private y: number) {}

  toString(): string {
    return `${this.x},${this.y}`;
  }
}

console.log(`At ${new Point(1, 2)}`);
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `ignoredTypeNames` | `["Error", "RegExp", "URL", "URLSearchParams"]` | Names of types that are never reported. Types that extend them aren't reported either. Your list replaces the default. |
| `checkUnknown` | `false` | Also report values of type `unknown`, which may be plain objects. |

{/* good {"options": [{"ignoredTypeNames": ["Money"]}]} */}
```ts
interface Money {
  amount: number;
}

declare const price: Money;

console.log(`Total: ${price}`);
```

{/* bad {"options": [{"checkUnknown": true}]} */}
```ts
function describe(value: unknown): string {
  return `Got ${value}`;
}
```
