numberMethodRanges
Reports when number method arguments are outside their valid range.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
Number methods such as toExponential, toFixed, toPrecision, and toString have specific valid ranges for their arguments.
Passing values outside these ranges will throw a RangeError at runtime.
This rule flags numeric literal arguments that are outside the valid range:
Number.prototype.toExponential(digits): digits must be between 0 and 100Number.prototype.toFixed(digits): digits must be between 0 and 100Number.prototype.toPrecision(precision): precision must be between 1 and 100Number.prototype.toString(radix): radix must be between 2 and 36
Examples
Section titled “Examples”const const binary: string
binary = (255).Number.toString(radix?: number): string
Returns a string representation of an object.
toString(1);const const hex: string
hex = (255).Number.toString(radix?: number): string
Returns a string representation of an object.
toString(37);const const fixed: string
fixed = (3.14159).Number.toFixed(fractionDigits?: number): string
Returns a string representing a number in fixed-point notation.
toFixed(101);const const exponential: string
exponential = (12345).Number.toExponential(fractionDigits?: number): string
Returns a string containing a number represented in exponential notation.
toExponential(-1);const const precision: string
precision = (12345).Number.toPrecision(precision?: number): string
Returns a string containing a number represented either in exponential or fixed-point notation with a specified number of digits.
toPrecision(0);const const binary: string
binary = (255).Number.toString(radix?: number): string
Returns a string representation of an object.
toString(2);const const hex: string
hex = (255).Number.toString(radix?: number): string
Returns a string representation of an object.
toString(16);const const fixed: string
fixed = (3.14159).Number.toFixed(fractionDigits?: number): string
Returns a string representing a number in fixed-point notation.
toFixed(2);const const exponential: string
exponential = (12345).Number.toExponential(fractionDigits?: number): string
Returns a string containing a number represented in exponential notation.
toExponential(5);const const precision: string
precision = (12345).Number.toPrecision(precision?: number): string
Returns a string containing a number represented either in exponential or fixed-point notation with a specified number of digits.
toPrecision(10);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase is a rare one that overrides those native methods, this rule might not be for you.
Further Reading
Section titled “Further Reading”- MDN: Number.prototype.toExponential()
- MDN: Number.prototype.toFixed()
- MDN: Number.prototype.toPrecision()
- MDN: Number.prototype.toString()
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Oxlint:
oxc/number-arg-out-of-range