Skip to content

Bug: [prefer-optional-chain] doesn't report at all when requireNullish is true #8487

Closed
@auvred

Description

@auvred

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

https://typescript-eslint.io/play/#ts=5.3.3&fileType=.ts&code=CYUwxgNghgTiAEYD2A7AzgF3gMyUgXPJjAJYoDm8APvCgK4QQDcAULkvAGSc54B0GJAGUMpCgAoAlEyA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WloHsalfkwCG8WmQAWo5ujABtcNhy9o-aJAA0ylVhXYYiAI6xKnAHIJGyKfPxxEu7AF9nAXWVuXQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declare const foo: string | null;
foo && foo.toString();

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/prefer-optional-chain": [
      "error",
      {
        "requireNullish": true
      }
    ]
  }
}

tsconfig

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

Expected Result

I believe that this case should be reported. (see Additional Info below)

Actual Result

This case isn't reported.

Additional Info

I noticed this while working on #8382: there is an example of incorrect code with requireNullish: true - https://typescript-eslint.io/rules/prefer-optional-chain#requirenullish. But it doesn't actually contain any lint errors.

As I understand it, requireNullish just restricts the set of types of the left operand of a logical expression to types that are nullish. If we disable requireNullish, then the rule reports on the code provided above (playground).

Therefore, I believe that this case should be reported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: 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