Look at the input:
parseInt(string: string, radix?: number): number; ^^^^^^
The first argument must be a string. This is in accordance with the specification:
parseInt (string , radix)
The parseInt function creates an integer value determined by the interpretation of the contents of the string argument in accordance with the specified base.
In regular JS, the first argument is coerced into a string based on the following rule in the specification:
- Let inputString be ToString (string).
therefore parseInt(1.2) works.
Please note that the specification allows radix to be undefined , which coincides with the absence of it, so the question mark in the radix?: number signature. In this case, of course, by default it is 10 (if the line does not look like 0xabc ).
As mentioned in other answers, parseInt not the best solution anyway, if what you really want to do is an operation on the floor or truncation.
user663031
source share