oxc-project/oxc · warning
Unexpected implicit coercion to string
Error message
Unexpected implicit coercion to string
What it means
The string-kind diagnostic of oxlint's no-implicit-coercion rule. It flags template literals whose only content is a single interpolation (`${value}`), string concatenation like value + '' or '' + value, and value += ''. The rule wants String(value) so the conversion is explicit; templates that contain literal text around interpolations are fine.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_implicit_coercion.rs:29
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)]
enum CoercionKind {
Boolean,
Number,
String,
}
/// Reports an implicit coercion with an auto-fix suggestion
fn report_coercion(ctx: &LintContext, span: Span, kind: CoercionKind, operand: &str) {
let (diagnostic, wrapper) = match kind {
CoercionKind::Boolean => (boolean_coercion_diagnostic(span), "Boolean"),
CoercionKind::Number => (number_coercion_diagnostic(span), "Number"),
CoercionKind::String => (string_coercion_diagnostic(span), "String"),View on GitHub (pinned to e1e7af627c)
Solutions
- Use String(value) for the conversion.
- Keep template literals only when they contain literal text around the interpolation.
- Disable string checks in the rule options if template style is intentional.
Example fix
// before
const key = `${user.id}`;
// after
const key = String(user.id); Defensive patterns
Strategy: type-guard
Validate before calling
const strCast = /(?:`\$\{[^}]+\}`|\+\s*''|''\s*\+)/.test(source); Type guard
const asStr = (v: unknown): string => (typeof v === 'string' ? v : String(v));
Prevention
- Use String(x) for explicit string conversion.
- Build object keys with String(x) instead of `${x}` alone.
- Reserve template literals for strings with literal text around interpolations.
When it happens
Trigger: const key = `${user.id}`;; '' + flags; text += ''; concatenating x + '' to build object keys or log lines.
Common situations: Building object keys from ids; stringifying numbers or symbols for logging; the '' + x trick carried over from older snippets.
Related errors
- Unexpected implicit coercion to boolean
- Unexpected implicit coercion to number
- Unexpected string concatenation of literals.
- Unnecessary escape character {escape_char:?}
- Empty array binding pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/620bb7c4ac447b9a.
Report an issue: GitHub.