{"record":{"id":"0fbd3667892fbfd1","repo":"oxc-project/oxc","slug":"this-number-literal-will-lose-precision-at-runtime","errorCode":null,"errorMessage":"This number literal will lose precision at runtime.","messagePattern":"This number literal will lose precision at runtime\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs","lineNumber":12,"sourceCode":"use std::borrow::Cow;\n\nuse cow_utils::CowUtils;\nuse oxc_ast::{AstKind, ast::NumericLiteral};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_loss_of_precision_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"This number literal will lose precision at runtime.\")\n        .with_help(\n            \"Use a number literal representable by a 64-bit floating-point number, or use a `BigInt` literal (for example, `9007199254740993n`) for exact large integers.\",\n        )\n        .with_note(\n            \"In JavaScript, `Number` values exactly represent integers only in the range -9007199254740991 to 9007199254740991 (`Number.MIN_SAFE_INTEGER` to `Number.MAX_SAFE_INTEGER`). `BigInt` supports arbitrarily large integers.\",\n        )\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoLossOfPrecision;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow precision loss in numeric literals.\n    ///\n    /// ### Why is this bad?","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs#L1-L30","documentation":"Diagnostic from oxlint's eslint/no-loss-of-precision rule (crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs:12). It reports a numeric literal whose value cannot be represented exactly as an IEEE-754 double: Number only represents integers exactly up to 9007199254740991 (2**53 - 1), and most decimal fractions are inexact. The literal silently becomes a different number at runtime, so the source code lies about the value (e.g. 9007199254740993 evaluates to 9007199254740992).","triggerScenarios":"Integer literals beyond Number.MAX_SAFE_INTEGER (const id = 9007199254740993;); literals with more decimal digits than a double can hold (const x = 0.10000000000000000001; evaluates to 0.1); very long literals copied from databases, snowflake IDs, or documentation.","commonSituations":"Hard-coding Twitter/X snowflake IDs, big user IDs, or blockchain amounts as Number literals; porting constants from Python/Java (arbitrary/big integers) to JS; test fixtures copy-pasted with full-precision expected values.","solutions":["Append n to make it a BigInt literal when exact integer arithmetic is needed: 9007199254740993n","Otherwise choose the nearest representable value explicitly (9007199254740992) so source and runtime agree","For IDs from external systems, keep them as strings end-to-end instead of numeric literals"],"exampleFix":"// before\nconst tweetId = 9007199254740993; // evaluates to 9007199254740992\n\n// after\nconst tweetId = 9007199254740993n; // BigInt, exact\n// or, if it must be a Number:\nconst tweetId = '9007199254740993'; // string ID","handlingStrategy":"validation","validationCode":"// Guard for hardcoded numeric IDs/constants in codegen or templates\nfunction assertExactAsNumber(literal) {\n  const n = Number(literal);\n  if (Number.isInteger(n) && !Number.isSafeInteger(n)) {\n    throw new RangeError(`${literal} loses precision as Number; use BigInt or string`);\n  }\n}\nassertExactAsNumber(9007199254740993);","typeGuard":"function isExactlyRepresentable(literal) {\n  const n = Number(literal);\n  return Number.isInteger(n) ? Number.isSafeInteger(n) : String(n).length >= String(literal).replace(/^0+|0+$/g, '').length - 1;\n}","tryCatchPattern":null,"preventionTips":["Treat external IDs as strings from the API boundary inward","Use BigInt (n suffix) for values beyond 2**53 - 1","Enable no-loss-of-precision in CI so silent rounding cannot land"],"tags":["lint","javascript","numbers","ieee-754","precision","bigint","correctness"],"backgroundTag":"number-precision-loss","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"}