Skip to content

Using "||" causes lint errors for nullish checks #760

@samyarkd

Description

@samyarkd

Describe the bug

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.eslint[@typescript-eslint/prefer-nullish-coalescing](https://typescript-eslint.io/rules/prefer-nullish-coalescing)

Using || over ?? for nullish checks is bad practice and it's better to use ?? when possible, also it causes lint errors in projects where a rule to prevent it is configured.

Using || over ?? for nullish checks is bad practice and it's better to use ?? when possible, also it causes lint errors in projects where a rule to prevent it is configured.

To Reproduce
Steps to reproduce the behavior:

  • Open a TypeScript/JavaScript file in the project.

  • Write code using || for default values, e.g. const value = input || "default".

  • Run ESLint or trigger linting in your editor.

  • See error from @typescript-eslint/prefer-nullish-coalescing.

Expected behavior
The linter should not throw an error when using the safer nullish coalescing operator (??). Code should be written as const value = input ?? "default" instead of using ||.

OS: Windows

Browser: Edge Version 139.0.3405.111 (Official build) (64-bit)

Additional context
This issue can be fixed by:

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions