Skip to main content

no-non-null-asserted-optional-chain

Disallow non-null assertions after an optional chain expression.

💡

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

foo?.bar!;
foo?.bar()!;

Further Reading

Options

This rule is not configurable.

Resources