简体   繁体   English

Javascript + JsDoc:如何记录像地图这样的新ES6数据类型?

[英]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: 我正在尝试在我的ES6项目中使用JSDoc,我正在返回一个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 ? 我应该如何用@returns记录这个?

There is not good answer here . 没有很好的答案在这里

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> . 基本模式是Map<KeyType, ValueType> From your example, key would be a string and value an object. 从您的示例中,key将是一个字符串并且值为一个对象。 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> . 然后你的地图将被声明为Map<String, MyObject> Cool, isn't, it? 很酷,不是吗? You could also nest other maps or even sets, like Map<Number, Set<MyObject>> . 您还可以嵌套其他地图甚至集合,例如Map<Number, Set<MyObject>>

Your best bet is probably to define Map and friends with @external somewhere: 你最好的选择可能是在@external某处定义Map和朋友:

/**
 * 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 defined, so you can say @returns {Map} as the comments say. 然后你的类型将被定义,所以你可以像评论所说的那样说@returns {Map}

A convenient place to get all the URLs and one-liners in one place is from tern . 在一个地方获取所有URL和单行的便利位置来自tern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM