2016-07-12 57 views
5

Sto cercando di utilizzare JSDoc nel mio progetto ES6, sto tornando una mappa:Javascript + JsDoc: come documentare nuovi tipi di dati ES6 come la mappa?

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

Come devo documentare questo con @returns?

Non c'è una buona risposta here.

+4

@returns {Mappa}? – Thomas

+0

Ho un problema quasi simile. https://stackoverflow.com/questions/38309123/jsdoc-with-and-immutable-js-datastructures-in-annotations –

risposta

2

La risposta è semplice e bella:

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

Il modello di base è Map<KeyType, ValueType>. Dal tuo esempio, la chiave sarebbe una stringa e valuterà un oggetto. Potresti anche andare avanti e dichiarare anche il tuo oggetto. Per esempio:

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

E poi il tuo mappa sarebbe stata dichiarata come Map<String, MyObject>. Fantastico, no? Puoi anche nidificare altre mappe o persino set, come Map<Number, Set<MyObject>>.

0

La cosa migliore è probabilmente quello di definire Map e gli amici con @external da qualche parte:

/** 
* 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} 
*/ 

Poi saranno definiti il ​​tipo, in modo da poter dire @returns {Map} come dicono i commenti.

Un luogo comodo per ottenere tutti gli URL e le battute in un'unica posizione è da tern.