Closure Compiler - warning: limited index type found: string, required: number

I get this warning:

WARNING - restricted index type

found   : string
required: number

someArray[ index ].doSomething();

This happens after updating the closing compiler to the latest version. It seems that using indexes of type string for arrays is not recommended by the closure compiler.

What would be the recommended solution to this problem?

BTW. Is there a way to disable checking for these types of alerts (I looked at the list of CC checkboxes and found nothing)?

+4
source share
1 answer

If your index variable has a type string, you must parse it first. Try

someArray[parseInt(index)].doSomething();

, , , , , - DOM- HTML-. , , , .

const parsedIndex = parseInt(index);
if (isNaN(parsedIndex) || index < 0) {
  throw 'Invalid index';
}
someArray[parsedIndex].doSomething();
-1

Source: https://habr.com/ru/post/1651270/


All Articles