Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[no-unnecessary-condition] False positive unnecessary optional chain on nestedvoid values #9754

Open
Labels
accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@lukekarrys

Description

@lukekarrys

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

packageversion
@typescript-eslint/eslint-plugin8.0.1
TypeScript5.5.4
ESLint9.8.0
node22.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: 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


      [8]ページ先頭

      ©2009-2025 Movatter.jp