Skip to content

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.

const binary = parseInt("111110111", 2);
const octal = parseInt("767", 8);
const hex = parseInt("1F7", 16);
const value = Number.parseInt("FF", 16);

This rule is not configurable.

If your project heavily deals with literals and you want to be consistent with their parsing, this rule might not be useful for you.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.