Search code examples
javascriptecmascript-6jsdocjsdoc3

Javascript + JsDoc: How to document new ES6 datatypes like map?


I'm trying to use JSDoc in my ES6 project, I'm 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 should I document this with @returns?

There is not good answer here.


Solution

  • 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;
    }
    

    The basic pattern is Map<KeyType, ValueType>. From your example, key would be a string and value an object. You could even go ahead and declare your object as well. For instance:

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

    And then your map would be declared as Map<String, MyObject>. Cool, isn't, it? You could also nest other maps or even sets, like Map<Number, Set<MyObject>>.