Skip to content

Bug: [dot-notation] Conflict between noPropertyAccessFromIndexSignature and extra (constrained) object keys #10627

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
kachkaev opened this issue Jan 7, 2025 · 1 comment · Fixed by #10644
Closed
4 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

@kachkaev
Copy link

kachkaev commented Jan 7, 2025

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.7.2&fileType=.ts&code=C4TwDgpgBAogHsATgQwNIRFAvFABhBFAEgG8BnJASwDsBzAX1wG4AoF0SKAZSQFcBjYL0QQA6pWAALeEmRlsUEiyhQAZgHt1ALigVENWqxUAjZIh3HNAGwjJqRqAG0Cs9CB0yUbgLo7qvAFtjCERWejZ%2BdWoKXT5BYTEJaUI5HR5EASERcSlPOQUlFQ1tKAAiSQgrK3VSgBplKFNzNWQrMgh6lRcUAEFjfh0AFgAmTqhu5AARCFUh0ZZwlkjo9RsAOmraAAo9TISc5NkyNeKASiWoslWIDfVt3fjspLzjpvOLlfXNnbisxNyUscJn1%2BOdllcvncfhlHv9DiggSlpqp3uDrrd7r99s9AY5SsD%2BqVvGDLujvg8-gcXniJsiiacgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6AEwHs6nuBDfJU5N0URNGidokcGAC%2BIOUA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkRsACprIlngJ4CC006JJAMWbwAkkQAm6AB4BlZAHMiAQzwBXTOgpg8mVenBgAviENA&tokens=false

Repro Code

type ExtraKey = `extra${string}`;

type StructureWithExtras = {
  foo: string;
  bar: boolean;
  [extraKey: ExtraKey]: number;
}

const structureWithExtras: StructureWithExtras = {
  foo: "hello",
  bar: false,
  extraAbc: 42,
  extraDef: 42,
}

console.log(structureWithExtras.foo)
console.log(structureWithExtras.bar)

console.log(structureWithExtras.extraAbc)
console.log(structureWithExtras.extraDef)

console.log(structureWithExtras["extraAbc"])
console.log(structureWithExtras["extraDef"])

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/dot-notation": "error",
  },
};

tsconfig

{
  "compilerOptions": {
    "noPropertyAccessFromIndexSignature": true
  }
}

Expected Result

I expected structureWithExtras["extraAbc"] to not produce ["extraAbc"] is better written in dot notation

Actual Result

  • structureWithExtras.extraAbc is not liked by tsc
  • structureWithExtras["extraAbc"] is not liked by @typescript-eslint/dot-notation

Additional Info

Just using Record<ExtraKey, number> produces the same deadlock (Playground):

type ExtraKey = `extra${string}`;

type StructureWithJustExtras = Record<ExtraKey, number>;

const structureWithJustExtras: StructureWithJustExtras = {
  extraAbc: 42,
  extraDef: 42,
};

console.log(structureWithJustExtras.extraAbc);
console.log(structureWithJustExtras.extraDef);

console.log(structureWithJustExtras["extraAbc"]);
console.log(structureWithJustExtras["extraDef"]);

Replacing Record<ExtraKey, number> with Record<string, number> helps (Playground):

type StructureWithStringKeys = Record<string, number>;

const structureWithStringKeys: StructureWithStringKeys = {
  extraAbc: 42,
  extraDef: 42,
};

console.log(structureWithStringKeys.extraAbc);
console.log(structureWithStringKeys.extraDef);

console.log(structureWithStringKeys["extraAbc"]);
console.log(structureWithStringKeys["extraDef"]);

However, using Record<string, number> would not be possible in the original example where number value is used alongside string and boolean.

@kachkaev kachkaev 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 Jan 7, 2025
@kachkaev kachkaev changed the title Bug: [dot-notation] Conflict between noPropertyAccessFromIndexSignature and extra object keys Bug: [dot-notation] Conflict between noPropertyAccessFromIndexSignature and constrained object keys Jan 7, 2025
@kachkaev kachkaev changed the title Bug: [dot-notation] Conflict between noPropertyAccessFromIndexSignature and constrained object keys Bug: [dot-notation] Conflict between noPropertyAccessFromIndexSignature and extra (constrained) object keys Jan 7, 2025
@bradzacher
Copy link
Member

the bug here is due to [extrakey: ExtraKey].
TS's getStringIndexType() method does not treat this indexer as a string indexer and so the rule doesn't see the indexer at all -- hence it reports.

This seems like a bug / limitation in TS's API?
We might have to change strategy to inspect the node's indexInfos and look for indexers with keyType.flags & TypeFlags.StringLike?

@bradzacher bradzacher 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 Jan 7, 2025
@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 Feb 11, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 11, 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
2 participants