18

When I write the following code, the annotator tells me that BrowserSelector is not defined in the second typedef:

/**
 * @typedef {{name: String, minVer: Number, maxVer: Number}} BrowserSelector
 */

/**
 * @typedef {{type:String, browser: BrowserSelector, attribute: Object}} Selector
 */

I believe it is not associating the type with the name. How can I do that?

I would prefer not to add actual code for it, just jsdoc comments.

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
gztomas
  • 3,030
  • 3
  • 27
  • 38

3 Answers3

21

I'm using this comment style for 'struct' like types:

/**
 * @name BrowserSelector
 * @property {String} name
 * @property {Number} minVer
 * @property {Number} maxVer
 */

/** @type {BrowserSelector|*} */
var mySelector = {}; // no warning because of '*' in @type :S
mySelector.name = 'foo'; // no warning :)
mySelector.id = 'bar'; // warning :)

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
iolo
  • 1,157
  • 9
  • 8
  • That's the structure I am currently using, because Webstorm v8 still doesn't seem to understand @typedef. – Lucio Paiva Jul 27 '14 at 23:15
  • Allegedly, [`@typedef support was fixed](https://youtrack.jetbrains.com/issue/WEB-11189) but I found it's still not working in WebStorm 10.0.4. – Dan Dascalescu Jul 30 '15 at 08:59
12

Multiple comments to describe properties is not necessary as of Webstorm 7 (perhaps earlier).

/**
 * @name BrowserSelector
 * @type {{
 *     name: String,
 *     minVer: Number,
 *     maxVer: Number
 * }}
 */
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
8

I recently noticed in the AngularJS source code that they also annotated stuff without any directly attached code. I tried the same principle on your case and came up with the following (even code-completion and type checking work with it in WebStorm):

/**
 * @name BrowserSelector
 */
/**
 * @name BrowserSelector#name
 * @type {string}
 */
/**
 * @name BrowserSelector#minVer
 * @type {number}
 */
/**
 * @name BrowserSelector#maxVer
 * @type {number}
 */

/**
 * @name Selector
 */
/**
 * @name Selector#type
 * @type {string}
 */
/**
 * @name Selector#browser
 * @type {BrowserSelector}
 */
/**
 * @name Selector#attribute
 * @type {Object}
 */
marius
  • 7,766
  • 18
  • 30
Emil van Galen
  • 304
  • 1
  • 2