question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

parseInt() incorrect number of arguments

See original GitHub issue
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

console.log(parseInt("123"));

results in a warning

JSC_WRONG_ARGUMENT_COUNT: Function parseInt: called with 1 argument(s). Function requires at least 2 argument(s) and no more than 2 argument(s).

which is not correct. The radix argument for parseInt is optional and can be omitted.

Searching through why Closure wants to have two arguments there leads to Google-specific convention at

https://github.com/google/closure-compiler/blob/9c3912131abd29dbb2b264e423077d261f3c1fb1/externs/es3.js#L361-L372

It is a bit unfortunate that coding conventions from a specific team/company overrule published standards.

But no biggie, following the advice and adding undefined, i.e.

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

console.log(parseInt("123",undefined));

results in no warning, but produces

console.log(parseInt("123",void 0));

which is suboptimal with the , void 0 bloating up the code size. (also the code could have been optimized to just console.log(123), but that’s beyond the interest of this issue)

Tried to use my own --externs file to fix this problem, and tried

/**
 * @param {string} str
 * @param {number=} radix
 * @return {number}
 * @suppress {duplicate}
 */
var parseInt = function(str, radix) {};

but that gave trouble with

WARNING - [JSC_TYPE_MISMATCH] initializing variable
found   : function(string, number=): number
required: function(*, (number|undefined)): number
var parseInt = function(str, radix) {};
               ^^^^^^^^^^^^^^^^^^^^^^^

Then tried with

/**
 * @param {string} str
 * @param {number=} radix
 * @return {number}
 * @suppress {duplicate}
 */
var parseInt = /** function(*, (number|undefined)): number */ (function(str, radix) {});

and with

var parseInt = /** function(*, number=): number */ (function(str, radix) {});

but neither of them want to operate since externs.zip/es3.js already contains an earlier definition.

How can I make Closure be optimal sized and warnings-free when it sees code like parseInt("123")?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jujcommented, Feb 15, 2020

You always have the option of suppressing type checking “@Suppress {checkTypes}”

Thanks! Tried with

/**@suppress {checkTypes}*/ return parseInt(str);
return parseInt(/**@suppress {checkTypes}*/str);
return /**@suppress {checkTypes}*/parseInt(str);
return /**@suppress {checkTypes}*/(parseInt(str));

and

  /**@suppress {checkTypes}*/
  $jstoi_q: function(str) {
    return parseInt(str);
  },

The four first versions gave an error

WARNING - [JSC_MISPLACED_SUPPRESS] @suppress annotation not allowed here. See https://github.com/google/closure-compiler/wiki/@suppress-annotations
 /**@suppress {checkTypes}*/ return parseInt(str);
                             ^^^^^^^^^^^^^^^^^^^^^

The last one did not, but it still prints the warning

WARNING - [JSC_WRONG_ARGUMENT_COUNT] Function parseInt: called with 1 argument(s). Function requires at least 2 argument(s) and no more than 2 argument(s).
 /**@suppress {checkTypes}*/ return (parseInt(str));
                                     ^^^^^^^^^^^^^

Tried reading through https://github.com/google/closure-compiler/wiki/@suppress-annotations but was not able to figure it out 😕 I wonder what is the right syntax here?

0reactions
jujcommented, Feb 26, 2020

I’d expect this to have worked for you

Thanks! I reissued that test, and it does actually work. I suspect I botched something up when trying the first time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: error when using parseInt() on a number
error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. Am I missing something here or is it an...
Read more >
parseInt() doesn't always correctly convert to integer - 2ality
parseInt() has the following signature: parseInt(value, radix?) It converts value to string, ignores leading whitespace and then parses as many ...
Read more >
How to fix: "TypeError: Incorrect number of arguments or ...
This error tells you that you're trying to call the sum() function with the wrong number of arguments. Here's an example of a...
Read more >
parseInt() - JavaScript - MDN Web Docs
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
Read more >
Error messages - FICO
The subroutine myprint is called with a real-valued argument instead of an integer. E-103 Incorrect number of subscripts for `array'(num1/num2). An array is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found