Urara-Blog/node_modules/.pnpm-store/v3/files/2b/c13ebd362baa3f62a2ca7b5addcf96d89bc86e5ef7cfabf730dffce7d6407f8faec11f4f76f3e3ecfec8956b521740f23614d16e1ae05fa795f0a9a0f4538a
2022-08-14 01:14:53 +08:00

50 lines
1,005 B
Text

---
description: 'Enforce non-null assertions over explicit type casts.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/non-nullable-type-assertion-style** for documentation.
This rule detects when an `as` cast is doing the same job as a `!` would, and suggests fixing the code to be an `!`.
## Rule Details
Examples of code for this rule:
<!--tabs-->
### ❌ Incorrect
```ts
const maybe = Math.random() > 0.5 ? '' : undefined;
const definitely = maybe as string;
const alsoDefinitely = <string>maybe;
```
### ✅ Correct
```ts
const maybe = Math.random() > 0.5 ? '' : undefined;
const definitely = maybe!;
const alsoDefinitely = maybe!;
```
## Options
```jsonc
// .eslintrc.json
{
"rules": {
"@typescript-eslint/non-nullable-type-assertion-style": "warn"
}
}
```
This rule is not configurable.
## When Not To Use It
If you don't mind having unnecessarily verbose type casts, you can avoid this rule.