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 51c186a3ba3d..0bf19d4a3127 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 2d8b24585827..3be6810957f4 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 { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy