Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bug: [prefer-nullish-coalescing] Should not report on false | undefined values by default #11092

Open
Labels
breaking changeThis change will require a new major version to be releasedbugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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 havesearched for related issues and found none that matched my issue.
  • I haveread the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.8.2&fileType=.ts&code=CYUwxgNghgTiAEYD2A7AzgF3gWygTwCMQAueAcijPgB9yCyBuAKFElgWXSwDMoIICUMAGtSAbTJgyAXWatoceNwCuKMBgCWqRHCgYQANT7KQaABQBKUphgaUAczHSa8VaG52QwZkxVrN2sjYAA7K%2BgDKGLYO5lbwNnaOzgDeTPCIqJjwdvow2F4aeggAvDj4RPDFVeSU8ABkdTogRUYQJrE%2B6ZxZAG7GJdkoufnAhfo0tLz8giKd8HAYyjAo8H1tIMwAvkxAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6KL2jjokcGAC%2BILUA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declareconstmaybe:'a'|'b';declareconstfallback:['c'];declarefunctioncreateValues():string[]|undefined;functioncomputeStrings():string[]{constintermediate=maybe==='a'&&createValues();constvalue=intermediate||fallback;returnvalue;}

ESLint Config

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

tsconfig

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

Expected Result

intermediate isfalse | string[] | undefined and its parent function returnsstring[]. The|| is the right operator to use to makevalue astring[], not astring[] | false.

Actual Result

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator. 8:30 - 8:32

Additional Info

Related:#7055,#10356, and probably others, searching gives a lot of false positive results...

#10356 notes that this gets suggested a good amount. I personally was surprised to re-discover that the default behavior of the rule is to report in a way that is sometimes incorrect/unnecessary.

Proposal: I think we should switch the rule to ignorefalse | undefined | (always-truthy)s by default.

💖

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeThis change will require a new major version to be releasedbugSomething 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


      [8]ページ先頭

      ©2009-2025 Movatter.jp