Pascal
Pascal

Reputation: 2974

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?

Upvotes: 10

Views: 2506

Answers (2)

Mateusz Jamiołkowski
Mateusz Jamiołkowski

Reputation: 543

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.

Upvotes: 10

jmbucknall
jmbucknall

Reputation: 2061

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.

Upvotes: 6

Related Questions