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 2b260865a4c..535692f3fa8 100644 --- a/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts +++ b/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts @@ -1,5 +1,5 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; -import { AST_NODE_TYPES } from '@typescript-eslint/utils'; +import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils'; import { createRule } from '../util'; @@ -67,7 +67,7 @@ export default createRule({ if (parentId) { const scope = context.getScope(); - const superVar = scope.set.get(parentId.name); + const superVar = ASTUtils.findVariable(scope, parentId.name); if (superVar) { const isCircular = superVar.references.some( item => 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 5c59f92b92d..6bdc76362e0 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 @@ -39,7 +39,16 @@ interface Foo { [key: string]: Foo; } `, - + ` +interface Foo { + [key: string]: Foo; +} + `, + ` +interface Foo { + [key: string]: Foo | string; +} + `, // Type literal 'type Foo = {};', ` @@ -328,6 +337,17 @@ type Foo = Readonly>; }, { code: ` +interface Foo { + [k: string]: T; +} + `, + output: ` +type Foo = Record; + `, + errors: [{ messageId: 'preferRecord', line: 2, column: 1 }], + }, + { + code: ` interface Foo { [k: string]: A.Foo; }