diff --git a/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts b/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts index 51c186a3ba3..0bf19d4a312 100644 --- a/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts +++ b/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts @@ -205,11 +205,10 @@ export default createRule({ const scope = context.sourceCode.getScope(key); const superVar = ASTUtils.findVariable(scope, parentId.name); if (superVar) { - const isCircular = superVar.references.some( - item => - item.isTypeReference && - node.range[0] <= item.identifier.range[0] && - node.range[1] >= item.identifier.range[1], + const isCircular = isDeeplyReferencingType( + node.parent, + superVar, + new Set([parentId]), ); if (isCircular) { return; @@ -291,6 +290,12 @@ function isDeeplyReferencingType( return [node.indexType, node.objectType].some(type => isDeeplyReferencingType(type, superVar, visited), ); + case AST_NODE_TYPES.TSMappedType: + if (node.typeAnnotation) { + return isDeeplyReferencingType(node.typeAnnotation, superVar, visited); + } + + break; case AST_NODE_TYPES.TSConditionalType: return [ node.checkType, diff --git a/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts b/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts index 2d8b2458582..3be6810957f 100644 --- a/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts @@ -174,6 +174,18 @@ interface ExampleObject { [key: string]: ExampleRoot; } `, + ` +type Bar = { + [k in K]: Bar; +}; + `, + ` +type Bar = { + [k in K]: Foo; +}; + +type Foo = Bar; + `, // Type literal 'type Foo = {};', @@ -646,6 +658,36 @@ type Foo2 = Record; type Foo3 = Record>; `, }, + { + code: ` +type Foos = { + [k in K]: { foo: Foo }; +}; + +type Foo = Foos; + `, + errors: [{ column: 39, line: 2, messageId: 'preferRecord' }], + output: ` +type Foos = Record; + +type Foo = Foos; + `, + }, + { + code: ` +type Foos = { + [k in K]: Foo[]; +}; + +type Foo = Foos; + `, + errors: [{ column: 39, line: 2, messageId: 'preferRecord' }], + output: ` +type Foos = Record; + +type Foo = Foos; + `, + }, // Generic {