diff --git a/packages/eslint-plugin/src/rules/prefer-readonly.ts b/packages/eslint-plugin/src/rules/prefer-readonly.ts index 19597264bbcb..1768dc71663e 100644 --- a/packages/eslint-plugin/src/rules/prefer-readonly.ts +++ b/packages/eslint-plugin/src/rules/prefer-readonly.ts @@ -3,6 +3,7 @@ import * as ts from 'typescript'; import * as util from '../util'; import { typeIsOrHasBaseType } from '../util'; import { ASTUtils, AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; +import { isIntersectionType } from 'tsutils'; type MessageIds = 'preferReadonly'; type Options = [ @@ -270,8 +271,12 @@ class ClassScope { classNode: ts.ClassLikeDeclaration, private readonly onlyInlineLambdas?: boolean, ) { - this.checker = checker; - this.classType = checker.getTypeAtLocation(classNode); + const classType = checker.getTypeAtLocation(classNode); + if (isIntersectionType(classType)) { + this.classType = classType.types[0]; + } else { + this.classType = classType; + } for (const member of classNode.members) { if (ts.isPropertyDeclaration(member)) { diff --git a/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts b/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts index c1602f019134..e0f1c73c74dc 100644 --- a/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts @@ -292,6 +292,19 @@ class Foo { }, { code: ` +function ClassWithName {}>(Base: TBase) { + return class extends Base { + private _name: string; + + public test(value: string) { + this._name = value; + } + }; +} + `, + }, + { + code: ` class Foo { private value: Record = {};