persona-community-5/.pnpm-store/v3/files/db/6940ea78dafa3e349cf9965d6002e4ee356205b5fc1725bad01cc0c8f87546e7c106fa8f503946ccda87de0bffc2e2825796100d13a84631b644d8c538f47e
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

95 lines
2.6 KiB
Plaintext

---
description: 'Require `.toString()` to only be called on objects which provide useful information when stringified.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-base-to-string** for documentation.
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()` uses the format `"[object Object]"`, which is often not what was intended.
This rule reports on stringified values that aren't primitives and don't 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.
## Examples
<Tabs>
<TabItem value="❌ Incorrect">
```ts
// 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();
```
</TabItem>
<TabItem value="✅ Correct">
```ts
// 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}`;
```
</TabItem>
</Tabs>
## Options
### `ignoredTypeNames`
A string array of type names to ignore, this is useful for types missing `toString()` (but actually has `toString()`).
There are some types missing `toString()` in old version TypeScript, like `RegExp`, `URL`, `URLSearchParams` etc.
The following patterns are considered correct with the default options `{ ignoredTypeNames: ["RegExp"] }`:
```ts option='{ "ignoredTypeNames": ["RegExp"] }' showPlaygroundButton
`${/regex/}`;
'' + /regex/;
/regex/.toString();
let value = /regex/;
value.toString();
let text = `${value}`;
```
## When Not To Use It
If you don't mind a risk of `"[object Object]"` or incorrect type coercions in your values, then you will not need this rule.
## Related To
- [`restrict-plus-operands`](./restrict-plus-operands.mdx)
- [`restrict-template-expressions`](./restrict-template-expressions.mdx)
## Further Reading
- [`Object.prototype.toString()` MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString)