no-non-null-asserted-optional-chain
Disallow non-null assertions after an optional chain expression.
✅
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
💡
Some problems reported by this rule are manually fixable by editor suggestions.
?.
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.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-non-null-asserted-optional-chain": "error"
}
};
Examples
- ❌ Incorrect
- ✅ Correct
foo?.bar!;
foo?.bar()!;
foo?.bar;
foo?.bar();
Further Reading
Options
This rule is not configurable.