Skip to content

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

Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions