Javascript + JsDoc: how to document new ES6 data types such as map?

I am trying to use JSDoc in my ES6 project, I am returning a map:

/** * Some documentation.. * * @returns {undefined} <- This should be replaced */ function returningMap() { const someMap = new Map(); someMap.set("key", {a, b, c}); return someMap; } 

How do I document this with @returns ?

There is no good answer here .

+5
source share
2 answers

The answer is simple and beautiful:

 /** * Some documentation. * * @return {Map<String, Object>} */ function returningMap() { const someMap = new Map(); someMap.set("key", {a, b, c}); return someMap; } 

Main template Map<KeyType, ValueType> . In your example, the key will be the string and value of the object. You can even go and declare your object. For instance:

 /** * @typedef {Object} MyObject * @property {Number} a * @property {Number} b * @property {String} c */ 

And then your map will be declared as Map<String, MyObject> . Cool, right? You can also nest other maps or even sets, for example Map<Number, Set<MyObject>> .

+2
source

It is best to define Map and @external friends somewhere:

 /** * The Map object is a simple key/value map. Any value (both objects and primitive values) may be used as either a key or a value. * @external Map * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map} */ 

Then your type will be determined, so you can tell @returns {Map} as the comments say.

A convenient place to get all the URLs and single-line links in one place is tern .

0
source

All Articles