Bug: [prefer-optional-chain] Incorrect fix for chain ending in generic comparison

Author: anderskCreated Sep 25, 2026Updated Oct 1, 2026
Labelsbugpackage: eslint-pluginaccepting prs

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.
  • If I used AI assistance to prepare this issue, I have reviewed and followed the AI Contribution Policy.

Playground Link

https://typescript-eslint.io/play/#ts=6.0.3&fileType=.ts&code=GYVwdgxgLglg9mABMAPAFQHwAphzgLkQG9EAjAQwCcB%2BQsEAW1IFNLEBfRAH0XABNmwGGGZ8ANIgBuhNAEpiAKESJKzKCEpJccRADJdyPADoKbALwWpAbgXsFEBAGc4AG2ZGXcAOY4s-QcKiEv5CInyysjZAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WloHsalfkwCG8WmQAWo5uii9o-aJHBgAviHVA&tsconfig=N4XyA&tokens=false

Repro Code

typescript
function f<T>(foo: { bar?: number } | undefined, v: T) {
  return foo && foo.bar === v;
}
console.log(f(undefined, undefined));

ESLint Config

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

tsconfig

jsonc
{}

Expected Result

No error.

Actual Result

@typescript-eslint/prefer-optional-chain suggests changing foo && foo.bar === v to foo?.bar === v. This evaluates to a different result when foo === undefined and v === undefined.

Additional Info

The rule already knows to suppress the report when undefined is explicitly listed as part of the type: v: undefined or v: T | undefined. But nothing stops an unconstrained v: T from being undefined as well, so the report should be suppressed in that case too.

The same issue occurs for v: void.

Source: typescript-eslint/typescript-eslint