diff --git a/packages/eslint-plugin/src/rules/no-base-to-string.ts b/packages/eslint-plugin/src/rules/no-base-to-string.ts index 34fa2c7d6dc..b1593c23d8b 100644 --- a/packages/eslint-plugin/src/rules/no-base-to-string.ts +++ b/packages/eslint-plugin/src/rules/no-base-to-string.ts @@ -112,6 +112,18 @@ export default util.createRule({ return Usefulness.Always; } + if (type.isIntersection()) { + for (const subType of type.types) { + const subtypeUsefulness = collectToStringCertainty(subType); + + if (subtypeUsefulness === Usefulness.Always) { + return Usefulness.Always; + } + } + + return Usefulness.Never; + } + if (!type.isUnion()) { return Usefulness.Never; } diff --git a/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts b/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts index 86babb99ae1..542097aa9cb 100644 --- a/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts +++ b/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts @@ -107,6 +107,12 @@ tag\`\${{}}\`; function tag() {} tag\`\${{}}\`; `, + ` + interface Brand {} + function test(v: string & Brand): string { + return \`\${v}\`; + } + `, ], invalid: [ { @@ -217,5 +223,23 @@ tag\`\${{}}\`; }, ], }, + { + code: ` + interface A {} + interface B {} + function test(intersection: A & B): string { + return \`\${intersection}\`; + } + `, + errors: [ + { + data: { + certainty: 'will', + name: 'intersection', + }, + messageId: 'baseToString', + }, + ], + }, ], });