Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Open
Description
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 havesearched for related issues and found none that matched my issue.
- I haveread the FAQ and my problem is not listed.
Issue Description
Given the following code, I would expect it to lint successfully:
constmaybe=<T>(v:T):T|void=>vconsta=maybe({key1:{key2:maybe({i:1,}),},})?.key1.key2?.i// ^// 9:14 error Unnecessary optional chain on a non-nullish value @typescript-eslint/no-unnecessary-condition
In my reproduction the second optional chain is removed if I run with--fix
but then TypeScript errors with the following:
> tsc --noEmitindex.ts:9:15 - error TS2339: Property 'i' does not exist on type 'void | { i: number; }'. Property 'i' does not exist on type 'void'.9 })?.key1.key2.i ~Found 1 error in index.ts:9
I also want to note that this behavior does not occur if the value could benull
onlyvoid
. It successfully lints if I change the return ofmaybe
toT | null
.
In my reproduction I also tried to narrow it down as far as I could and found that these examples lint successfully:
// This lints fineconstb=maybe({key1:maybe({i:1,}),})?.key1?.i// So does thisconstc={key1:{key2:maybe({i:1,}),},}.key1.key2?.i
Reproduction Repository Link
https://gist.github.com/lukekarrys/979ea0dd68b52937e9a257b4d2c9c293
Repro Steps
git clone https://gist.github.com/lukekarrys/979ea0dd68b52937e9a257b4d2c9c293npm installnpm run lint
Versions
package | version |
---|---|
@typescript-eslint/eslint-plugin | 8.0.1 |
TypeScript | 5.5.4 |
ESLint | 9.8.0 |
node | 22.4.0 |