Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let Visual Studio 2010 JavaScript IntelliSense know the type of object

Let's say I have the javascript function below:

  function (msg) {
    var divForResult = document.getElementById("test");
    if (typeof (msg) == "object")
    {
      divForResult.innerHTML = "Result: <b>" + msg.Message + "</b>";
    }
    else {
      divForResult.innerHTML = "Result: <b>" + msg + "</b>";
    }
  }

I know that if the msg variable is an object, it's as Exception, so I print the Message property. If not, the msg is a string, and I print the variable itself. My question is how do I let Visual Studio 2010 JavaScript IntelliSense "know" the type of object msg is, so that I'll get the correct properties/functions for the object type in a situation such as this?

like image 696
Pascal Avatar asked Dec 18 '10 20:12

Pascal


2 Answers

Actually it's not limited to local variables. You can help VS by using xml comments like this:

function foo(message) {
    /// <param name="message" type="String"></param>
    message. //ctr+space here
}

It is not exactly what you are asking for, but it works great when you are accepting argument of one type only.

like image 84
Mateusz Jamiołkowski Avatar answered Sep 30 '22 05:09

Mateusz Jamiołkowski


Unfortunately, Visual Studio's "pseudo-execution" of JavaScript in order to provide better Intellisense support is still not comprehensive enough.

For example, I wrote this little function:

var foo = function(obj) {
  if (typeof obj === "string") {
    // presumably Intellisense should know obj is a string 
    // in this compound statement but it doesn't.
    // try "obj." here
  }

  if ((typeof obj === "object") && (obj.constructor === Date)) {
    // presumably Intellisense should know obj is a Date 
    // in this compound statement but it doesn't.
    // try "obj." here
  }

};

And if you try it out VS2010 doesn't notice that in the two clauses I've tried to limit the type of the passed-in object and that therefore it could provide better suggestions. So, it seems that Intellisense is pretty limited to local variables.

like image 44
jmbucknall Avatar answered Sep 30 '22 06:09

jmbucknall