src/content/docs/linter/rules/no-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/style/noNonNullAssertion`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`@typescript-eslint/no-non-null-assertion`](https://typescript-eslint.io/rules/no-non-null-assertion){
"linter": {
"rules": {
"style": {
"noNonNullAssertion": "error"
}
}
}
}
Disallow non-null assertions using the ! postfix operator.
TypeScript's ! non-null assertion operator asserts to the type system that an expression is non-nullable, as
in not null or undefined. Using assertions to tell the type system new information is often a sign that
code is not fully type-safe. It's generally better to structure program logic so that TypeScript understands
when values may be nullable.
interface Example {
property?: string;
}
declare const foo: Example;
const includesBaz = foo.property!.includes('baz');
(b!! as number) = "test";
interface Example {
property?: string;
}
declare const foo: Example;
const includesBaz = foo.property?.includes('baz') ?? false;