如何在 JSDoc 中记录字典?

下一个例子:

var CONF = {
locale: {
"en": {
name: "English",
lang: "en-US"
},
"es": {
name: "Spanish",
lang: "es-ES"
}
}
};

而且知道 locale 属性包含的是一个来自数据库的 dictionary 对象,我如何用 JDoc 记录它的内部属性?

目前我正在考虑为我的区域设置对象使用 typedef类型,那么我是否可以将 locale属性简单地设置为一个已定义类型的 Array?这样做对吗?

40706 次浏览

As far as I can tell:

Using @typedef and @property to define a custom type is the "correct" way in JSDoc. But it is cumbersome to write and ugly to read (a cardinal sin in documentation).

The record type is much neater (note the double \{\{s):

   /** \{\{
name:string,
lang:string
}} */

According to the JSDoc 3 docs:

Arrays and objects (type applications and record types)

An object with string keys and number values:

{Object.<string, number>}

So it would be:

/** @type \{\{locales: Object.<string, {name: string, lang: string}>}} */
var CONF = {
locales: {
en: {
name: "English",
lang: "en-US"
},
es: {
name: "Spanish",
lang: "es-ES"
}
}
};

Cleaner, using @typedef

/**
* @typedef \{\{name: string, lang: string}} locale
*/
/**
* @type \{\{locales: Object.<string, locale>}}
*/
var CONF = {
locales: {
en: {
name: "English",
lang: "en-US"
},
es: {
name: "Spanish",
lang: "es-ES"
}
}
};