Skip to content

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

@kachkaev

Description

@kachkaev

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.

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy