JSDocs / Intellisense question WS9

I have the property of a class declared in the constructor like this:

 
/**
* Associative array of GridConfig class
* @name View#grids
* @type {Object.<string, GridConfig>}
*/

  this.grids={};

From google docs {Object.<string, GridConfig>} should declare grids as an associative array with string key and values of type B

and I am using it somewhere like this:

view.grids.grdResults.refresh();



So intellisense correctly picks up views and it also notes grids as implicitly defined, it has no intellisense for grdResults and for refresh just finds everything in the app that has a refresh method

What I want is for the intellisense to realize that grdResults is of type GridConfig and detect its refresh method, is this the right way to declare it?


Tks
Rob

5 comments
Comment actions Permalink

Please provide the full code sample. Your Object type should be marked as generic type using '@template A,B' annotation to be able to be instantiated as

 
Object.<string, GridConfig> and have grdResults of type {B}
0
Comment actions Permalink
 
Hi
 
This is a stripped version of the code but includes the relevant bit... 
  
/**
* The View context
* @class View
* @param {string} type The view type name
* @param {View} owner The owning View, Panels and Popups are owned, Pages aren't
* @param {string} name The view name or id
* @constructor
*/
var View = function(type, owner, name){


....... LOTS OF OTHER PROPERTIES.......

 
/**
* Associative array of HTMLFormElements
* @name View#forms
* @type {Object.<string, HTMLFormElement>}
*/
this.forms={};

   
/**
* Associative array of GridConfig
* @name View#grids
* @type {Object.<string, GridConfig>}
*/
this.grids={};



}

 
/**
* Adds a grid config to the view grids
* @param {string} id
* @returns {GridConfig}
*/
View.prototype.addGrid = function(id){
    return this.grids[id] ? this.grids[id] : this.grids[id] = new GridConfig(id);
};
0
Comment actions Permalink

I am not really sure that @template is appropriate here.

The type of value in the grids associate array never changes it will always be a GridConfig , similarly the forms array will always hold HTMLFormElements.

From what I read of @template it is where the type might vary so you specify it at the point it is instantiated, but in this case there might be 20 View objects but in every case grids property is always an associative array holding objects of type GridConfig, what varies with each instance is the name i.e. the key  

0
Comment actions Permalink

Haven't heard of such syntax - please can you point me to the corresponding documentation? From Google JSDoc documentation, @type {Foo.<string>} syntax is used for creating instance of generic type Foo (https://developers.google.com/closure/compiler/docs/js-for-compiler#generics).
WebStorm supports JSDoc generic types, but has no idea of associative arrays holding objectsbeing used as types

0
Comment actions Permalink

https://developers.google.com/closure/compiler/docs/js-for-compiler

{Array.<string>}

An array of strings.

{Object.<string, number>}

An object in which the keys are strings and the values are numbers.



Actually it seems WS9 does support this JSDocs syntax in one way:

views.grids['myGrid'].refresh()

but not if I do this

views.grids.myGrid.refresh()



in the first case it correctly realizes ['myGrid'] is a GridConfig class and provides intellisense for it, in the second case it does not.

So WS9 can actually document associative arrays containing a single type but only if you reference using array index syntax ['aName'] rather direct accessing the value with dot notation .aName which most people are going to do.
0

Please sign in to leave a comment.