noBaseToString
Summary
Section titled “Summary”- Rule available since:
v2.4.15 - Diagnostic Category:
lint/nursery/noBaseToString - This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noBaseToString": "error" } } }}Description
Section titled “Description”Require stringification to avoid values that only use the default object representation.
JavaScript coerces values to strings in several places, such as String(value),
value.toString(), string concatenation, template interpolation, and Array#join().
When the value only inherits the default object stringification, that often produces
"[object Object]" instead of something intentionally readable.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const value: {} = {};String(value);/invalid-string.ts:2:8 lint/nursery/noBaseToString ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This expression will use the default object stringification format, such as [object Object], when stringified.
1 │ const value: {} = {};
> 2 │ String(value);
│ ^^^^^
3 │
ℹ Stringify a primitive value, define a custom stringification method, or serialize the object explicitly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const value: {} = {};`${value}`;/invalid-template.ts:2:4 lint/nursery/noBaseToString ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This expression will use the default object stringification format, such as [object Object], when stringified.
1 │ const value: {} = {};
> 2 │ `${value}`;
│ ^^^^^
3 │
ℹ Stringify a primitive value, define a custom stringification method, or serialize the object explicitly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const values: {}[] = [{}];values.join(",");/invalid-join.ts:2:1 lint/nursery/noBaseToString ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This join() call will stringify one or more elements with the default object format, such as [object Object].
1 │ const values: {}[] = [{}];
> 2 │ values.join(”,”);
│ ^^^^^^
3 │
ℹ Ensure every joined element has a useful string representation, or map the values before joining.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
String(1);class CustomToString { toString() { return "ok"; }}
`${new CustomToString()}`;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.