Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [prefer-nullish-coalescing] reports when expressions are not equivalent #11306

Closed as not planned
@miguel-leon

Description

@miguel-leon

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.8.2&fileType=.ts&code=CYUwxgNghgTiAEYD2A7AzgF3gMyUgXPJjAJYoDmA3ALABQdoksC2ArimBiajigBS4CRDKQoBKQilYBbAEYgY8AD7x2obGRDAa9WsnRZZseAF4ceeADJLvAXjE748APTOnAPQD8dfZnhGAL1N-YyUVAA4AFh0gA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6KL2jjokcGAC%2BILUA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declare const foo: string;

declare function fn(foo: string): number | undefined;

const bar = foo && fn(foo);
  //  ^?
const baz = bar || 84;

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/prefer-nullish-coalescing": "error"
  }
}

tsconfig

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

Expected Result

Should not report since the expressions are not equivalent and could produce a runtime error.

Actual Result

Reports error on the last line.

Additional Info

The expresion before || could be something other than a nullish value ('' or 0), so the operator shouldn't be changed to ??.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfix: user errorissue was fixed by correcting the configuration / correcting the codepackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions