Urara-Blog/node_modules/.pnpm-store/v3/files/5c/2d72b64e3a210e623950f5d6882a2b952b734f75c3bd1fa1fe3ae6c23773c01624da98446f6f50f5e79521f16ebaf38c2eb1b1dc4e7d4a426c6f33a0839b27
2022-08-14 01:14:53 +08:00

72 lines
1.6 KiB
Text

---
description: 'Disallow non-null assertions in the left operand of a nullish coalescing operator.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-non-null-asserted-nullish-coalescing** for documentation.
## Rule Details
The nullish coalescing operator is designed to provide a default value when dealing with `null` or `undefined`.
Using non-null assertions in the left operand of the nullish coalescing operator is redundant.
Examples of code for this rule:
<!--tabs-->
### ❌ Incorrect
```ts
/* eslint @typescript-eslint/no-non-null-asserted-nullish-coalescing: "error" */
foo! ?? bar;
foo.bazz! ?? bar;
foo!.bazz! ?? bar;
foo()! ?? bar;
let x!: string;
x! ?? '';
let x: string;
x = foo();
x! ?? '';
```
### ✅ Correct
```ts
/* eslint @typescript-eslint/no-non-null-asserted-nullish-coalescing: "error" */
foo ?? bar;
foo ?? bar!;
foo!.bazz ?? bar;
foo!.bazz ?? bar!;
foo() ?? bar;
// This is considered correct code because there's no way for the user to satisfy it.
let x: string;
x! ?? '';
```
## Options
```jsonc
// .eslintrc.json
{
"rules": {
"@typescript-eslint/no-non-null-asserted-nullish-coalescing": "warn"
}
}
```
This rule is not configurable.
## When Not To Use It
If you are not using TypeScript 3.7 (or greater), then you will not need to use this rule, as the nullish coalescing operator is not supported.
## Further Reading
- [TypeScript 3.7 Release Notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html)
- [Nullish Coalescing Proposal](https://github.com/tc39/proposal-nullish-coalescing)