{"record":{"id":"1ef53338b06ce4ad","repo":"oxc-project/oxc","slug":"unexpected-implicit-coercion-to-number","errorCode":null,"errorMessage":"Unexpected implicit coercion to number","messagePattern":"Unexpected implicit coercion to number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_implicit_coercion.rs","lineNumber":23,"sourceCode":"use oxc_str::CompactStr;\nuse oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn boolean_coercion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected implicit coercion to boolean\")\n        .with_help(\"Use `Boolean(value)` instead\")\n        .with_label(span)\n}\n\nfn number_coercion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected implicit coercion to number\")\n        .with_help(\"Use `Number(value)` instead\")\n        .with_label(span)\n}\n\nfn string_coercion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected implicit coercion to string\")\n        .with_help(\"Use `String(value)` instead\")\n        .with_label(span)\n}\n\n/// Type of implicit coercion being detected\n#[derive(Clone, Copy)]\nenum CoercionKind {\n    Boolean,\n    Number,\n    String,\n}\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_implicit_coercion.rs#L5-L41","documentation":"The number-kind diagnostic of oxlint's no-implicit-coercion rule. It flags unary plus +value and multiplication by one (value * 1, 1 * value), the shorthand coercions to number, and recommends the explicit Number(value) call. Options control whether number coercion is checked and which shorthand forms are allowed.","triggerScenarios":"const n = +input.value;; total = raw * 1;; 1 * qty used to force numeric strings into numbers; +el.dataset.count passed around as a number.","commonSituations":"Parsing DOM inputs, dataset attributes, and query-string params where unary plus is the habitual trick; old code-golf snippets copied from forums; a strictness sweep turning the rule on mid-project.","solutions":["Use Number(value) for straight conversion.","Use Number.parseInt(value, 10) or Number.parseFloat when parsing strings (usually the real intent).","Allow the + shorthand via the rule's allow option if it is team style.","Turn off number checks in the rule options."],"exampleFix":"// before\nconst count = +inputEl.value;\n\n// after\nconst count = Number.parseInt(inputEl.value, 10);","handlingStrategy":"type-guard","validationCode":"const plusCast = /[=(,[]\\s*\\+\\s*[A-Za-z_(]/.test(source);","typeGuard":"const asNum = (v: unknown): number => (typeof v === 'number' ? v : Number(v));","tryCatchPattern":null,"preventionTips":["Use Number(x), Number.parseInt, or Number.parseFloat for conversions.","Avoid unary plus on values in linted code.","Configure the allow list if the +x shorthand is accepted team style."],"tags":["eslint","oxlint","lint","coercion","number"],"backgroundTag":"implicit-number-coercion","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}