Skip to content

Docs: [prefer-nullish-coalescing] doesn't work with strictNullChecks: false. #5748

@jsalvata

Description

@jsalvata

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=4.8.4&sourceType=module&code=DYUwLgBAHgXBDOYBOBLAdgcwgHwgVzQBMQAzdEQgbgCgodcByBmoA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6KL2jjokcGAC+ILUA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylAGYBDVCXTgwAXxCygA

Repro Code

let x: string | undefined;
x || '';

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": false
  }
}

Expected Result

An error should be reporting for the ||, which should be replaced with ??.

Actual Result

No error is reported.

Additional Info

The same rule works if strictNullChecks is changed to true.

This makes sense because strictNullChecks: false essentially erases | null and | undefined from all types.

Ideally the rule should work anyway, assuming all types to be nullable. If that's not possible, a note should be added to the documentation so people do not spend a whole afternoon trying to understand why the rule doesn't work, as I did.

Versions

package version
@typescript-eslint/eslint-plugin 5.39.0
@typescript-eslint/parser 5.39.0
TypeScript 4.4.4
ESLint 7.32.0
node 12.22.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuedocumentationDocumentation ("docs") that needs adding/updatingfix: strictNullChecksissues that were fixed by turning on strictNullChecksgood first issueGood for newcomerspackage: 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