Skip to content

[consistent-indexed-object-style] fixer produces invalid code and deletes comments #4730

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
3 tasks done
Josh-Cena opened this issue Mar 25, 2022 · 4 comments · Fixed by #10490
Closed
3 tasks done
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

@Josh-Cena
Copy link
Member

Josh-Cena commented Mar 25, 2022

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

Playground

{
  "rules": {
    "@typescript-eslint/consistent-indexed-object-style": ["error", "index-signature"]
  }
}
type NavbarItems = Record<
  string,
  // lazy-load the component
  () => (props: any) => JSX.Element
>;

type NavbarItems = Record<
  Exclude<"a" | "b" | "c", "a">,
  string
>;

Expected Result

I expect the first to be fixed with comment preserved, and the second to be fixed to valid code:

// lazy-load the component
type NavbarItems = { [key: string]: () => (props: any) => JSX.Element };

type NavbarItems = { [key in Exclude<"a" | "b" | "c", "a">]: string };

Actual Result

// This has the comment swallowed
type NavbarItems = { [key: string]: () => (props: any) => JSX.Element };

// This should use mapped type instead of index signature
type NavbarItems = { [key: Exclude<"a" | "b" | "c", "a">]: string };

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 5.16.0
@typescript-eslint/parser 5.16.0
TypeScript 4.6.3
ESLint 8.11.0
node 17.5.0
@Josh-Cena Josh-Cena added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 25, 2022
@Josh-Cena
Copy link
Member Author

It is virtually impossible to determine whether mapped types or index signatures should be used if there are type aliases without making the rule type-aware. Maybe we should just use suggestions instead?

@bradzacher
Copy link
Member

We can certainly switch to suggestions if we can't reliably determine which should be used!
happy to accept a PR to make this one change

@bradzacher bradzacher added bug Something isn't working accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Mar 27, 2022
@Josh-Cena
Copy link
Member Author

I realized mapped types are more permissive than index access types. Mapped types also allow { [key in string]: number }. Is it possible that we only fix to index signature for TS{String,Number,Symbol}Keyword, and fix to mapped types for everything else, without getting rid of the fixer?

@bradzacher
Copy link
Member

we could definitely do that.
maybe we should do:

  1. use an autofixer if the keyword is string | number | symbol
  2. use a suggestion fixer in other cases

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