Skip to content

typescript/no-non-null-asserted-optional-chain Correctness

This rule is turned on by default.

What it does

Disallow non-null assertions after an optional chain expression.

Why is this bad?

?. 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.

Example

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

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny typescript/no-non-null-asserted-optional-chain
json
{
  "rules": {
    "typescript/no-non-null-asserted-optional-chain": "error"
  }
}

References

Released under the MIT License.