Skip to content

noBaseToString

biome.json
{
"linter": {
"rules": {
"nursery": {
"noBaseToString": "error"
}
}
}
}

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.

invalid-string.ts
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.

invalid-template.ts
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.

invalid-join.ts
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()}`;