Urara-Blog/node_modules/.pnpm-store/v3/files/7f/2125ebf3f599b6b3cffd465048b00cc92c19d10f078b9768065835e08ef88c889c70409e9eed3c8f0dab077174d26b3fd0a0ba15d3f48d13404213d0c39c87
2022-08-14 01:14:53 +08:00

67 lines
1.5 KiB
Text

---
description: 'Disallow non-null assertion in locations that may be confusing.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-confusing-non-null-assertion** for documentation.
## Rule Details
Using a non-null assertion (`!`) next to an assign or equals check (`=` or `==` or `===`) creates code that is confusing as it looks similar to a not equals check (`!=` `!==`).
```typescript
a! == b; // a non-null assertions(`!`) and an equals test(`==`)
a !== b; // not equals test(`!==`)
a! === b; // a non-null assertions(`!`) and an triple equals test(`===`)
```
<!--tabs-->
### ❌ Incorrect
```ts
interface Foo {
bar?: string;
num?: number;
}
const foo: Foo = getFoo();
const isEqualsBar = foo.bar! == 'hello';
const isEqualsNum = 1 + foo.num! == 2;
```
### ✅ Correct
<!-- prettier-ignore -->
```ts
interface Foo {
bar?: string;
num?: number;
}
const foo: Foo = getFoo();
const isEqualsBar = foo.bar == 'hello';
const isEqualsNum = (1 + foo.num!) == 2;
```
## Options
```jsonc
// .eslintrc.json
{
"rules": {
"@typescript-eslint/no-confusing-non-null-assertion": "warn"
}
}
```
This rule is not configurable.
## When Not To Use It
If you don't care about this confusion, then you will not need this rule.
## Further Reading
- [`Issue: Easy misunderstanding: "! ==="`](https://github.com/microsoft/TypeScript/issues/37837) in [TypeScript repo](https://github.com/microsoft/TypeScript)