src/content/docs/linter/rules/no-extra-non-null-assertion.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/suspicious/noExtraNonNullAssertion`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`@typescript-eslint/no-extra-non-null-assertion`](https://typescript-eslint.io/rules/no-extra-non-null-assertion){
"linter": {
"rules": {
"suspicious": {
"noExtraNonNullAssertion": "error"
}
}
}
}
Prevents the wrong usage of the non-null assertion operator (!) in TypeScript files.
The
!non-null assertion operator in TypeScript is used to assert that a value's type does not includenullorundefined. Using the operator any more than once on a single value does nothing.
const bar = foo!!.bar;
function fn(bar?: { n: number }) {
return bar!?.n;
}
function fn(bar?: { n: number }) {
return ((bar!))?.();
}
const bar = foo!.bar;
obj?.string!.trim();
function fn(key: string | null) {
const obj = {};
return obj?.[key!];
}