Skip to content

[consistent-indexed-object-style] Readonly modifier is ignored #2797

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
ryym opened this issue Nov 22, 2020 · 0 comments · Fixed by #2798
Closed
3 tasks done

[consistent-indexed-object-style] Readonly modifier is ignored #2797

ryym opened this issue Nov 22, 2020 · 0 comments · Fixed by #2798
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@ryym
Copy link
Contributor

ryym commented Nov 22, 2020

  • 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

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "rules": {
    "@typescript-eslint/consistent-indexed-object-style": "error"
  }
}
export type ReadonlyValues = {
  readonly [key: string]: number;
};

export const useValues = (vals: ReadonlyValues): void => {
  // @ts-expect-error
  vals.foo = 123;
};

tsconfig.json:

{
  "include": ["src"],
  "compilerOptions": {
    "noEmit": true,
    "strict": true
  }
}

Expected Result

ESLint reports no error. An index signature with readonly modifier cannot be converted to a Record type since the properties of Record type are not readonly.

Actual Result

ESLint reports an error.

$ npx eslint src/repro.ts

/private/tmp/repro/src/repro.ts
  1:30  error  A record is preferred over an index signature  @typescript-eslint/consistent-indexed-object-style

✖ 1 problem (1 error, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.

And fixing this error by eslint --fix makes the TypeScript compilation fail because now it is valid to assign a new value to vals.foo.

$ npx tsc
$ npx eslint --fix src/repro.ts
$ npx tsc
src/repro.ts(4,3): error TS2578: Unused '@ts-expect-error' directive.

Diff made by eslint --fix:

diff --git a/src/repro.ts b/src/repro.ts
index c64cb05..0971227 100644
--- a/src/repro.ts
+++ b/src/repro.ts
@@ -1,6 +1,4 @@
-export type ReadonlyValues = {
-  readonly [key: string]: number;
-};
+export type ReadonlyValues = Record<string, number>;
 
 export const useValues = (vals: ReadonlyValues): void => {
   // @ts-expect-error

Versions

package version
@typescript-eslint/eslint-plugin 4.8.1
@typescript-eslint/parser 4.8.1
TypeScript 4.1.2
ESLint 7.14.0
node 10.15.3
@ryym ryym added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Nov 22, 2020
@bradzacher bradzacher added bug Something isn't working has pr there is a PR raised to close this and removed triage Waiting for team members to take a look labels Nov 22, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants