Skip to content

Commit a76fa6e

Browse files
fix(eslint-plugin): [consistent-indexed-object-style] handle circular mapped types (typescript-eslint#10301)
1 parent a596501 commit a76fa6e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,24 @@ export default createRule<Options, MessageIds>({
176176
return;
177177
}
178178

179+
// If the mapped type is circular, we can't convert it to a Record.
180+
const parentId = findParentDeclaration(node)?.id;
181+
if (parentId) {
182+
const scope = context.sourceCode.getScope(key);
183+
const superVar = ASTUtils.findVariable(scope, parentId.name);
184+
if (superVar) {
185+
const isCircular = superVar.references.some(
186+
item =>
187+
item.isTypeReference &&
188+
node.range[0] <= item.identifier.range[0] &&
189+
node.range[1] >= item.identifier.range[1],
190+
);
191+
if (isCircular) {
192+
return;
193+
}
194+
}
195+
}
196+
179197
// There's no builtin Mutable<T> type, so we can't autofix it really.
180198
const canFix = node.readonly !== '-';
181199

packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface Foo {
3333
'type Foo = { [key: string]: string | Foo };',
3434
'type Foo = { [key: string]: Foo };',
3535
'type Foo = { [key: string]: Foo } | Foo;',
36+
'type Foo = { [key in string]: Foo };',
3637
`
3738
interface Foo {
3839
[key: string]: Foo;

0 commit comments

Comments
 (0)