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 and stylisticStrict presets.

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
const binary: number
binary
=
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

@paramstring A string to convert into a number.

@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.

parseInt
("111110111", 2);
const
const octal: number
octal
=
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

@paramstring A string to convert into a number.

@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.

parseInt
("767", 8);
const
const hex: number
hex
=
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

@paramstring A string to convert into a number.

@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.

parseInt
("1F7", 16);
const
const value: number
value
=
var Number: NumberConstructor

An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

Number
.
NumberConstructor.parseInt(string: string, radix?: number): number

Converts A string to an integer.

@paramstring A string to convert into a number.

@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.

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 ❤️‍🔥 around the world by the Flint team and contributors.