Skip to content

Bug: [no-unnecessary-condition] Improper checking of optional chaining of defined method return value #7912

@profaurore

Description

@profaurore

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

Playground Link

Repro Code

type T1 = { y: () => number } | null;
declare const x1: T1;
x1?.y()?.toExponential();
//     ^^
//     Error expected for unnecessary optional chaining.

type T2 = (() => number) | undefined;
declare const x2: T2;
x2?.()?.toExponential();
//    ^^
//     Error expected for unnecessary optional chaining.

type T3 = { y: number } | null;
declare const x3: T3;
x3?.y?.toExponential();
//   ^^
//   Error correctly caught.

type T4 = () => number;
declare const x4: T4;
x4()?.toExponential;
//  ^^
//  Error correctly caught.

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unnecessary-condition": "error",
  },
};

tsconfig

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

Expected Result

On lines 3 and 9, I expected there to be an error when using optional chaining on the return value of a method that returns a non-null and non-undefined value following another optional chaining.

Actual Result

On lines 3 and 9, no error was reported.

Additional Info

This is partially related to #1977 and #2111.

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

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy