Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript array : why is this not valid?

I have the following working code :

var routes = []; 

Eclipse validator for javascript prints the following warning :

Type mismatch: cannot convert from any[] to any 

What is wrong with my empty array ?

Edit : the warning disappeared later. Apparently Eclipse was wrong and the question needs to be closed. Sorry about that.

like image 354
Matthieu Napoli Avatar asked Apr 23 '11 18:04

Matthieu Napoli


People also ask

Why is my JavaScript array undefined?

You get undefined when you try to access the array value at index 0, but it's not that the value undefined is stored at index 0, it's that the default behavior in JavaScript is to return undefined if you try to access the value of an object for a key that does not exist.

Is array reference type in JavaScript?

Objects, arrays, and functions are reference types.

What is the correct way to JavaScript array?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

What is array () in JavaScript?

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.


1 Answers

Your JavaScript is valid, the problem is with JSDT plugin for Eclipse. In the latest version they introduced a type verification which is problematic in many situations - not only for empty arrays (like in your case). Another typical case may look like this: a = b || c; The plugin will complain when b and c are of different types (which is absolutely valid code for JavaScript). There is several bugs already reported to JSDT developers about this problem, but the issues are not fixed yet.

Unfortunately currently it is not possible to switch off the type verification using JSDT configuration screen in Eclipse. I switched it off directly from the JSDT source code. To do this, please follow these steps:

  1. Download the JSDT source code from Eclipse WebTools Project
  2. Open the org.eclipse.wst.jsdt.debug.core project with Eclipse. Make sure you have Eclipse SDK installed. It might also be required to adjust some dependencies in the plugin.xml file.
  3. The type verification is located in computeSeverity method of ProblemReporter class.
  4. To switch off type verification replace the line: case IProblem.TypeMismatch: return ProblemSeverities.Warning; with case IProblem.TypeMismatch: return ProblemSeverities.Ignore;
  5. Build the project and close Eclipse.
  6. In Eclipse folder find the file named org.eclipse.wst.jsdt.core<version>.jar - make a safe copy of it, then open the jar file and replace the ProblemReporter.class file with the one you compiled in the step 5 (the file is in bin folder of your project).
  7. Start Eclipse and clean your JavaScript project. All type checking will be ignored by JSDT.

Important! Make sure you've downloaded the same version of JSDT that you are using in Eclipse. Eventually instead of replacing a single file you can replace the entire plugin.

If you don't want to download and compile the plugin by yourself you can try with my fixed version. I've placed it on my FunctionSack webpage. I am using Eclipse 3.7 (Indigo) with JSDT 1.3.0, so make sure you have similar configuration if you want to use my file.

like image 74
ytropek Avatar answered Sep 20 '22 07:09

ytropek