Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
auvred opened this issue Feb 16, 2024 · 0 comments · Fixed by #8559
Closed
4 tasks done

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

auvred opened this issue Feb 16, 2024 · 0 comments · Fixed by #8559
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@auvred
Copy link
Member

auvred commented Feb 16, 2024

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.

@auvred auvred added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Feb 16, 2024
@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Feb 16, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants