no-loss-of-precision

Disallow number literals that cannot be stored exactly as written

error by default

A JavaScript number holds 15 to 17 significant digits. A literal with more is rounded without a warning. 9007199254740993 becomes 9007199254740992, so an ID typed into the code points at another record. A literal that's too large becomes Infinity, and one that's too small becomes 0.

A number with a fractional part isn't reported by default. The extra digits of 3.14159265358979323846 only round it to the closest number there is, which is what you'd expect.

This rule is on by default.

Configure#

On by default as an error. To turn it off:

bunfig.toml
[lint.rules]
"no-loss-of-precision" = "off" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-loss-of-precision" = ["error", {
  ignoreDecimals = true,
}]

Examples#

example.js4 problems
const orderId = 9007199254740993;
const mask = 0x20000000000001;
const huge = 1e400;
const tiny = 1e-400;
example.jsNo problems
const orderId = 9007199254740993n;
const orderKey = "9007199254740993";
const pi = 3.14159265358979323846;
const mask = 0x1fffffffffffff;
const huge = Infinity;

Options#

OptionDefaultWhat it does
ignoreDecimalstrueDon't report a number that has a fractional part.

Only bun lint has this option. With an ESLint config file decimals are reported, as in ESLint.

example.jswith { "ignoreDecimals": false }1 problem
const pi = 3.14159265358979323846;