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
- Replace !!value with Boolean(value).
- If the value is only used in a condition, drop the conversion entirely (if (value)).
- Configure the rule's allow option to permit !! if that is the accepted team style.
- 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
- Standardize on Boolean(x) for explicit boolean casts.
- Use typeof/instanceof narrowing instead of truthiness casts.
- Agree on the rule's allow list in .oxlintrc.json before enabling it repo-wide.
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
- Redundant Boolean call
- Unexpected implicit coercion to number
- Unexpected implicit coercion to string
- Empty array binding pattern
- Empty object binding pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/a78e837f5e0c2beb.
Report an issue: GitHub.