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
Repro Code
function f<T>(foo: { bar?: number } | undefined, v: T) {
return foo && foo.bar === v;
}
console.log(f(undefined, undefined));ESLint Config
module.exports = {
"rules": {
"@typescript-eslint/prefer-optional-chain": "error"
}
}tsconfig
{}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