Knockout intellisense in VS2010 when inside namespace

I have a javascript file containing my viewModel knockout. I added a link to the knockout debug file, but intellisense only works when I'm outside the namespace curly braces:

/// <reference path="knockout-2.0.0.debug.js" /> // YES! I get intellisense here :-) (function (window, $, ko, undefined) { // ... lots of js // NO intellisense in here :-( })(window, jQuery, ko); 

It would seem that ko in my namespace name signature "hides" the ko namespace. Is there a way associated with this other than changing my namespace signature:

 (function (window, $, myKo, undefined) { 

(Maybe I am missing something with namespaces? Do I even need to pass ko to my namespace?)

+4
source share
1 answer

Intellisense is not smart enough to follow the link. You may not need to enter links in your "namespace", as it does anyway. I would say that this is not even a namespace, it is just a closure. Given the normal use case, the namespace is a global container for bundling related code. An application can have one link in a global scope, and this will be considered as an application namespace. Other libraries, such as jQuery, will register their own values ​​in the global scope, which can be considered a namespace. In other cases, you can wrap the entire area in closure by registering namespaces in closure, so you never need to touch the global area.

I can understand why you might want to enter a link in your function. This view follows the interface template, so you can exchange these variables for mocked versions. Unfortunately, I do not think that you can conclude about Intellisense, and for the same reason (i.e. you can exchange it for anything), that it cannot determine exactly what these objects contain.

In a statically typed world, we actually define interface objects that allow Intellisense to reflect entered links, asserting that at least they have methods defined on the interface.

I think the only option you really need to keep Intellisense is to reference the global links that these libraries define.

Update

It looks like Microsoft actually used type annotations so that you can explicitly refer to the type of the function argument. I tested this version, implying a link using jQuery

 /// <reference path="jquery-1.6.2-vsdoc.js" /> (function ($) { /// <param name="$" type="jQuery"> /// A rererence to jQuery /// </param> })(jQuery); 

The jQuery version of intellisense declares a jQuery return type. Suppose Knockout has an intellisense that defines a type that you could annotate your function in the same way. In general, I think you can declare an object type as follows:

 var someObject = (function() { /// <returns type="someObject" /> // implementation here, returning object })(); function (a) { /// <summary> /// This function requires a to be a reference to a someObject type /// </summary> /// <param name="param" type="someObject"> /// A reference to a someObject type /// </param> /// <returns type="String" /> // we now have intellisense on a, treating it as someObject } 

So, if you are ready to go through the process of adding annotations, you can get Intellisense to work.

+2
source

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


All Articles