Shi*_*shi 5 javascript jsdoc ecmascript-6 jsdoc3
我正在尝试在我的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;
}
Run Code Online (Sandbox Code Playgroud)
我该如何记录@returns?
没有很好的答案在这里.
答案既简单又漂亮:
/**
* Some documentation.
*
* @return {Map<String, Object>}
*/
function returningMap() {
const someMap = new Map();
someMap.set("key", {a, b, c});
return someMap;
}
Run Code Online (Sandbox Code Playgroud)
基本模式是Map<KeyType, ValueType>。从您的示例中,键将是一个字符串,值是一个对象。你甚至可以继续声明你的对象。例如:
/**
* @typedef {Object} MyObject
* @property {Number} a
* @property {Number} b
* @property {String} c
*/
Run Code Online (Sandbox Code Playgroud)
然后您的地图将被声明为Map<String, MyObject>. 很酷,不是吗?您还可以嵌套其他地图甚至集合,例如Map<Number, Set<MyObject>>.