Skip to content

Bug: [prefer-optional-chain] convert autofixes that depend on type information to suggestions #11705

@kirkwaiblinger

Description

@kirkwaiblinger

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.9.3&fileType=.tsx&code=CYUwxgNghgTiAEYD2A7AzgF3gWygTwCMQBVFUAMwEsURgAueAb3iQAcNLUoIAFGNgPwNMMagHN4AX3gAfeAFcyIKjWABuAFDJ0WGAwBK4JDGAAeEeIA08CyjEA%2BeAF4mUtfAD0H%2BBhh5EABZQduI%2BSPCGyCbmvlY2sXayCkoqtPZaqJg4%2BESkFNS0AJIoAIIA6vgAKgDKACJIIGgoGNUgCC4wANoA5ADulBAQ8ETJ%2BaoAhN0AupqU5PAAFLiEJCkFwPAAZJvZK3nK6wB0bBxcvPyszk4uy7lrqsXlVXUNTS1tAJRMGvCe3gB6LBQEH8ADduJQNhhwtpQSAYFhobs7mNaAJjuxOChuHw2FcbjlVqjgI8KngavVGs1Wgg5vAfn9kUSDg9SmSKa9qW14JQ0PAAHIAeUqoxZtEOGkkQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WloHsalfkwCG8WmQAWo5uigB3UdCaRwYAL4gNQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declare const maybeUndefined: { optionalProp?: string } | undefined;
const r: Record<string, string> = { }; // try changing to Record<string, string | undefined>
const maybeUndefinedInAWayTSDoesntSee = r['will be undefined!'];
if (maybeUndefined && maybeUndefined.optionalProp === maybeUndefinedInAWayTSDoesntSee) {
  // ^ only valid to convert to maybeUndefined?.optionalProp === maybeUndefinedInAWayTSDoesntSee if 
  // maybeUndefinedInAWayTSDoesntSee is NOT undefined.
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/prefer-optional-chain": "warn"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

I expect that the report, which is highly dependent on type information for soundness, should generate a suggestion

Actual Result

An autofix was applied.

Additional Info

The use of autofixes rather than suggestions exacerbates bugs like #11700 and plays poorly with code that uses record/index/array types without noUncheckedIndexedAccess.

I propose to downgrade any autofix that is conditional on type information. Syntactic equivalents (or near enough) can remain autofixes for sure, such as const c = a != null ? a.b : undefined (which somehow we don't report??) can stay autofixes (if we actually have any non-type-dependent reports??)

We've done a similar process, eventually eliminating autofixes altogether, with strict-boolean-expressions. See #7312, #6173, #7743, #10136

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions