Skip to content

Commit 46d93a2

Browse files
authoredMar 11, 2025··
fix: support custom toString method in %s format (#7637)
1 parent 3fb3fbf commit 46d93a2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
 

‎packages/utils/src/display.ts

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export function format(...args: unknown[]): string {
110110
return '-0'
111111
}
112112
if (typeof value === 'object' && value !== null) {
113+
if (typeof value.toString === 'function' && value.toString !== Object.prototype.toString) {
114+
return value.toString()
115+
}
113116
return inspect(value, { depth: 0, colors: false })
114117
}
115118
return String(value)

‎test/core/test/utils-display.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ describe('format', () => {
1818
['%s', -0],
1919
['%s', null],
2020
['%s', null, 'next'],
21+
[
22+
'%s',
23+
new (class {
24+
constructor(public value: string) {}
25+
toString() {
26+
return this.value
27+
}
28+
})('string value'),
29+
],
2130
['%d', 100],
2231
['%d', 100n],
2332
['%d', null],

0 commit comments

Comments
 (0)
Please sign in to comment.