Skip to content

Bug: [consistent-indexed-object-style] Interface with recursive index signature autofixed to Record instead of mapped type #7863

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

Closed
4 tasks done
JoshuaKGoldberg opened this issue Oct 31, 2023 · 3 comments · Fixed by #10537
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JoshuaKGoldberg
Copy link
Member

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.2.2&fileType=.ts&code=KYDwDg9gTgLgBDAnmYcCiICGBbMAbYAVQDsBLCYuAXjgCMIIDNKAfOYgV21uCgG4AUANCRYCZKgw58wAEoN4NKbgIlyrdFhXAA8rQBWwAMYxBw8NHiliMXgDNMRyVpl7DJuAG8BcOAG0Aa2BEAC44AGcYKGsAcwBdMOUZeQhTAQBfASA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra1mAE0QAPRMNocARgCtEZOn0JJ0URNGgdokcGAC%2BIA0A&tsconfig=&tokens=false

Repro Code

export type ExampleUnion = boolean | number;

export type ExampleRoot = ExampleUnion | ExampleObject;

export interface ExampleObject {
  [key: string]: ExampleRoot;
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/<rule-name>": ["error", ...<options>],
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

This interface can be either be left as-is or auto-fixed to a mapped object type:

export type ExampleObject = {
  [key in string]: ExampleRoot;
};

Actual Result

It's actually autofixed to Record<string, ExampleRoot>, which causes TypeScript to give complaints that Type alias '(Example(Root|Object)' circularly references itself.

https://typescript-eslint.io/play/#ts=5.2.2&fileType=.ts&code=KYDwDg9gTgLgBDAnmYcCiICGBbMAbYAVQDsBLCYuAXjgCMIIDNKAfOYgV21uCgG4AUANCRYCZKgw58wAEoN4NKbgIlyrdFhXAA8rQBWwAMYxBw8NHhIUm6QT2GT1OLOPQAJgB4AzjCiliAHMAGltteQgYAD5BIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra1mAE0QAPRMNocARgCtEZOn0JJ0URNGgdokcGAC%2BIA0A&tsconfig=&tokens=false

Additional Info

I suspect this is in the same area as #4730 and #6598 but not quite the same.

Found while trying to enable the rule on typescript-eslint:

// Workaround for infinite type recursion
export interface JSONSchema4Object {
[key: string]: JSONSchema4TypeExtended;
}

@JoshuaKGoldberg JoshuaKGoldberg added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Oct 31, 2023
@Josh-Cena
Copy link
Member

The problem is, how do we detect if an interface is recursive? :(

@bradzacher
Copy link
Member

You can detect the simple case easily enough using scope analysis. I.e. "if the type has a reference within its range - it's self referencing".

Its a bit more involved if you want to consider indirect cycles though as you need to follow all aliases. Doable but painful.

@JoshuaKGoldberg
Copy link
Member Author

Doable but painful

Story of half my bug reports or feature requests... marking as accepting PRs for cases that can be easily found with scope manager.

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Nov 15, 2023
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Jan 7, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants