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 26eadce9983..53d50bd4694 100644 --- a/packages/eslint-plugin/src/rules/no-base-to-string.ts +++ b/packages/eslint-plugin/src/rules/no-base-to-string.ts @@ -25,6 +25,13 @@ export type Options = [ }, ]; export type MessageIds = 'baseArrayJoin' | 'baseToString'; +const canHaveTypeParameters = (declaration: ts.Declaration) => { + return ( + ts.isTypeAliasDeclaration(declaration) || + ts.isInterfaceDeclaration(declaration) || + ts.isClassDeclaration(declaration) + ); +}; export default createRule({ name: 'no-base-to-string', @@ -231,6 +238,17 @@ export default createRule({ return Usefulness.Always; } + const symbol = type.aliasSymbol ?? type.getSymbol(); + const decl = symbol?.getDeclarations()?.[0]; + if ( + decl && + canHaveTypeParameters(decl) && + decl.typeParameters && + ignoredTypeNames.includes(symbol.name) + ) { + return Usefulness.Always; + } + if (ignoredTypeNames.includes(getTypeName(checker, type))) { return Usefulness.Always; } 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 8c61eda34e6..7e6fa055b55 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 @@ -300,6 +300,30 @@ String(foo); `, options: [{ ignoredTypeNames: ['Foo'] }], }, + { + code: ` +interface MyError {} +declare const error: MyError; +error.toString(); + `, + options: [{ ignoredTypeNames: ['MyError'] }], + }, + { + code: ` +type MyError = {}; +declare const error: MyError; +error.toString(); + `, + options: [{ ignoredTypeNames: ['MyError'] }], + }, + { + code: ` +class MyError {} +declare const error: MyError; +error.toString(); + `, + options: [{ ignoredTypeNames: ['MyError'] }], + }, ` function String(value) { return value;