no-non-null-assertion
Disallow non-null assertions using the
!
postfix operator.
Attributes
- Included in configs
- ✅ Recommended
- 🔒 Strict
- Fixable
- 🔧 Automated Fixer
- 💡 Suggestion Fixer
- 💭 Requires type information
Rule Details
Using non-null assertions cancels the benefits of the strict null-checking mode.
Examples of code for this rule:
- ❌ Incorrect
- ✅ Correct
interface Foo {
bar?: string;
}
const foo: Foo = getFoo();
const includesBaz: boolean = foo.bar!.includes('baz');
interface Foo {
bar?: string;
}
const foo: Foo = getFoo();
const includesBaz: boolean = foo.bar?.includes('baz') ?? false;
Options
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-non-null-assertion": "warn"
}
};
This rule is not configurable.
When Not To Use It
If you don't care about strict null-checking, then you will not need this rule.