persona-community-5/.pnpm-store/v3/files/a6/387376f8f60ce20ab997eb068759c496554b1eaf46641a4cfdb8178a4a00ca3735bdfe36817c28e6e07caa2d456e430c1444ad3218079c48f631e8ab6480ab
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

48 lines
1.3 KiB
Plaintext

---
description: 'Enforce non-null assertions over explicit type casts.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/non-nullable-type-assertion-style** for documentation.
There are two common ways to assert to TypeScript that a value is its type without `null` or `undefined`:
- `!`: Non-null assertion
- `as`: Traditional type assertion with a coincidentally equivalent type
`!` non-null assertions are generally preferred for requiring less code and being harder to fall out of sync as types change.
This rule reports when an `as` cast is doing the same job as a `!` would, and suggests fixing the code to be an `!`.
## Examples
<Tabs>
<TabItem value="❌ Incorrect">
```ts
const maybe: string | undefined = Math.random() > 0.5 ? '' : undefined;
const definitely = maybe as string;
const alsoDefinitely = <string>maybe;
```
</TabItem>
<TabItem value="✅ Correct">
```ts
const maybe: string | undefined = Math.random() > 0.5 ? '' : undefined;
const definitely = maybe!;
const alsoDefinitely = maybe!;
```
</TabItem>
</Tabs>
## When Not To Use It
If you don't mind having unnecessarily verbose type assertions, you can avoid this rule.