oxc-project/oxc · warning · OxcDiagnostic

'Disallow non-null assertions in the left operand of a nulli

Error message

'Disallow non-null assertions in the left operand of a nullish coalescing operator

What it means

Oxlint's no-non-null-asserted-nullish-coalescing: `left! ?? right` first asserts the left operand is never null or undefined, then coalesces on nullish values, which is contradictory. The help at no_non_null_asserted_nullish_coalescing.rs:19 notes that `??` exists precisely to handle undefined and null, so the assertion is unnecessary. (The message string in this version literally begins with a stray apostrophe; that is a cosmetic typo in the source, not part of your code.)

Source

Thrown at crates/oxc_linter/src/rules/typescript/no_non_null_asserted_nullish_coalescing.rs:15

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::SymbolId;
use oxc_span::Span;
use oxc_syntax::operator::LogicalOperator;

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

fn no_non_null_asserted_nullish_coalescing_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("'Disallow non-null assertions in the left operand of a nullish coalescing operator")
        .with_help("The nullish coalescing operator is designed to handle undefined and null - using a non-null assertion is not needed.")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow non-null assertions in the left operand of a nullish coalescing operator.
    ///
    /// ### Why is this bad?
    ///
    /// The ?? nullish coalescing runtime operator allows providing a default value when dealing
    /// with `null` or `undefined`. Using a ! non-null assertion type operator in the left operand of
    /// a nullish coalescing operator is redundant, and likely a sign of programmer error or
    /// confusion over the two operators.

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the `!` and let `??` handle the nullish case
  2. If the value truly cannot be nullish, remove the `??` instead
  3. Use an explicit undefined check before the expression

Example fix

// before
const port = config.port! ?? 8080;

// after
const port = config.port ?? 8080;
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! ?? 'default'`, `(a.b)! ?? c` - a TSNonNullAssertion as the left operand of a nullish coalescing operator (LogicalOperator::Coalesce).

Common situations: Developers silencing 'possibly undefined' errors on the left of `??`; refactors where an assertion and a default were bolted on at different times.

Related errors


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