oxc-project/oxc · warning · OxcDiagnostic

extra non-null assertion

Error message

extra non-null assertion

What it means

Oxlint's no-extra-non-null-assertion flags redundant chained non-null assertions. A single `!` already strips null and undefined from the type, so chaining adds nothing; the note at no_extra_non_null_assertion.rs:20-24 explains precisely this. Each extra assertion on top of the first is reported.

Source

Thrown at crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs:17

use oxc_ast::{
    AstKind,
    ast::{ChainElement, Expression, match_member_expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{
    AstNode,
    ast_util::outermost_paren_parent,
    context::{ContextHost, LintContext},
    rule::Rule,
};

fn no_extra_non_null_assertion_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("extra non-null assertion")
        .with_help("Remove the redundant non-null assertion operator (`!`).")
        .with_note("The non-null assertion operator in TypeScript, written as `!`, tells the compiler that an expression is definitely not `null` or `undefined` at that point. Chaining multiple non-null assertions on the same expression does not provide any additional safety and is redundant.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoExtraNonNullAssertion;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow extra non-null assertions.
    ///
    /// ### Why is this bad?
    ///
    /// The `!` non-null assertion operator in TypeScript is used to assert that a value's type
    /// does not include `null` or `undefined`. Using the operator any more than once on a single value
    /// does nothing.

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Delete the extra `!` operators, keeping at most one
  2. If the value may genuinely be null or undefined, use `?.` or an explicit check instead of stacking assertions
  3. Run the linter with --fix in your editor so the redundant operator is stripped as you type

Example fix

// before
const name = user!!.name;

// after
const name = user?.name;
Defensive patterns

Strategy: validation

Validate before calling

npx oxlint --deny-warnings .

Type guard

function isDefined<T>(value: T | null | undefined): value is T {
  return value !== null && value !== undefined;
}

Prevention

When it happens

Trigger: `foo!!`, `foo!!.bar`, `(foo!)!`, `foo?.bar!!` - a TSNonNullAssertion whose inner expression is itself a non-null assertion.

Common situations: Copy-paste chains, code generation, developers unsure whether one `!` sufficed and adding another to silence the compiler.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/5c0de979f92e4e7c. Report an issue: GitHub.