Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-base-to-string] handle String()#10005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
68b31c7
cd22963
8b40290
addf652
8f4f8dc
711639f
80c39a0
0220cf0
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -9,7 +9,7 @@ import TabItem from '@theme/TabItem'; | ||
> | ||
> 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 whenconcatenated with a string (`expr + ''`), when interpolated into template literals (`${expr}`), or when passed as an argument to the String constructor (`String(expr)`). | ||
The default Object `.toString()` and `toLocaleString()` use 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()` or `toLocaleString()` method. | ||
@@ -31,6 +31,7 @@ value + ''; | ||
// Interpolation and manual .toString() and `toLocaleString()` calls too: | ||
`Value: ${value}`; | ||
String({}); | ||
({}).toString(); | ||
({}).toLocaleString(); | ||
``` | ||
@@ -44,6 +45,7 @@ value + ''; | ||
`Value: ${123}`; | ||
`Arrays too: ${[1, 2, 3]}`; | ||
(() => {}).toString(); | ||
String(42); | ||
(() => {}).toLocaleString(); | ||
// Defining a custom .toString class is considered acceptable | ||
@@ -64,6 +66,15 @@ const literalWithToString = { | ||
</TabItem> | ||
</Tabs> | ||
## Alternatives | ||
Consider using `JSON.stringify` when you want to convert non-primitive things to string for logging, debugging, etc. | ||
```typescript | ||
declare const o: object; | ||
const errorMessage = 'Found unexpected value: ' + JSON.stringify(o); | ||
``` | ||
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
## Options | ||
### `ignoredTypeNames` | ||
@@ -82,6 +93,7 @@ The following patterns are considered correct with the default options `{ ignore | ||
let value = /regex/; | ||
value.toString(); | ||
let text = `${value}`; | ||
String(/regex/); | ||
``` | ||
## When Not To Use It | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.