persona-community-5/.pnpm-store/v3/files/5b/c35f9e5feee917f9a8fae5c86fd76a68441953164c7f9e12303fdb540c033a373d1f2816cc7a03e4f429c4c8c42306ec5f90cca93d27f47571773478136a2f
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

47 lines
1.5 KiB
Plaintext

---
description: 'Disallow non-null assertions after an optional chain expression.'
---
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/no-non-null-asserted-optional-chain** for documentation.
`?.` optional chain expressions provide `undefined` if an object is `null` or `undefined`.
Using a `!` non-null assertion to assert the result of an `?.` optional chain expression is non-nullable is likely wrong.
> Most of the time, either the object was not nullable and did not need the `?.` for its property lookup, or the `!` is incorrect and introducing a type safety hole.
## Examples
<Tabs>
<TabItem value="❌ Incorrect">
```ts
foo?.bar!;
foo?.bar()!;
```
</TabItem>
<TabItem value="✅ Correct">
```ts
foo?.bar;
foo?.bar();
```
</TabItem>
</Tabs>
## When Not To Use It
If your project's types don't yet fully describe whether certain values may be nullable, such as if you're transitioning to `strictNullChecks`, this rule might create many false reports.
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
## Further Reading
- [TypeScript 3.7 Release Notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html)
- [Optional Chaining Proposal](https://github.com/tc39/proposal-optional-chaining/)