oxc-project/oxc · warning

Unexpected implicit coercion to boolean

Error message

Unexpected implicit coercion to boolean

What it means

The boolean-kind diagnostic of oxlint's no-implicit-coercion rule (ESLint port). It fires on double negation !!value, the idiomatic-but-implicit way to force a boolean, and asks for the explicit Boolean(value) call so the conversion is visible. Rule options can re-allow the !! shorthand and toggle which coercion kinds are checked.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_implicit_coercion.rs:17

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_str::CompactStr;
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};
use schemars::JsonSchema;
use serde::Deserialize;

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

fn boolean_coercion_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected implicit coercion to boolean")
        .with_help("Use `Boolean(value)` instead")
        .with_label(span)
}

fn number_coercion_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected implicit coercion to number")
        .with_help("Use `Number(value)` instead")
        .with_label(span)
}

fn string_coercion_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected implicit coercion to string")
        .with_help("Use `String(value)` instead")
        .with_label(span)
}

/// Type of implicit coercion being detected
#[derive(Clone, Copy)]

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace !!value with Boolean(value).
  2. If the value is only used in a condition, drop the conversion entirely (if (value)).
  3. Configure the rule's allow option to permit !! if that is the accepted team style.
  4. Disable boolean checks via the rule's kind options.

Example fix

// before
const hasItems = !!cart.length;

// after
const hasItems = Boolean(cart.length);
Defensive patterns

Strategy: type-guard

Validate before calling

const doubleBang = /(?:^|[^=!])!!(?![!=])/.test(source);

Type guard

const asBool = (v: unknown): boolean => (typeof v === 'boolean' ? v : Boolean(v));

Prevention

When it happens

Trigger: const flag = !!items;; const ok = !!result; returned from a function; !!document.hidden passed to an API expecting a boolean.

Common situations: Enabling the rule on an existing codebase full of !! idioms; style disputes between !!x and Boolean(x); the allow list not yet configured when the rule is first switched on.

Related errors


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