numericLiteralParsing
Reports parseInt calls with binary, hexadecimal, or octal strings that can be replaced with numeric literals.
✅ This rule is included in the ts stylistic
preset.
Binary, octal, and hexadecimal numeric literals provide a more readable and direct way to represent numbers in those bases compared to using parseInt()
or Number.parseInt()
with a radix argument.
Instead of parsing a string representation at runtime, you can use the native literal syntax (0b
for binary, 0o
for octal, 0x
for hexadecimal) which is clearer and more performant.
Examples
Section titled “Examples”const binary = parseInt("111110111", 2);
const octal = parseInt("767", 8);
const hex = parseInt("1F7", 16);
const value = Number.parseInt("FF", 16);
const binary = 0b111110111;
const octal = 0o767;
const hex = 0x1f7;
const value = 0xff;
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your project heavily deals with literals and you want to be consistent with their parsing, this rule might not be useful for you.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useNumericLiterals
- ESLint:
prefer-numeric-literals
- Oxlint:
eslint/prefer-numeric-literals
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.