Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(eslint-plugin): [consistent-indexed-object-style] check for indirect circular types in aliased mapped types #11177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,10 @@ export default createRule<Options, MessageIds>({
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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ interface ExampleObject {
[key: string]: ExampleRoot;
}
`,
`
type Bar<K extends string = never> = {
[k in K]: Bar;
};
`,
`
type Bar<K extends string = never> = {
[k in K]: Foo;
};

type Foo = Bar;
`,

// Type literal
'type Foo = {};',
Expand Down Expand Up @@ -646,6 +658,36 @@ type Foo2 = Record<string, Foo3>;
type Foo3 = Record<string, Record<string, Foo1>>;
`,
},
{
code: `
type Foos<K extends string = never> = {
[k in K]: { foo: Foo };
};

type Foo = Foos;
`,
errors: [{ column: 39, line: 2, messageId: 'preferRecord' }],
output: `
type Foos<K extends string = never> = Record<K, { foo: Foo }>;

type Foo = Foos;
`,
},
{
code: `
type Foos<K extends string = never> = {
[k in K]: Foo[];
};

type Foo = Foos;
`,
errors: [{ column: 39, line: 2, messageId: 'preferRecord' }],
output: `
type Foos<K extends string = never> = Record<K, Foo[]>;

type Foo = Foos;
`,
},

// Generic
{
Expand Down