How to document a dictionary in JSDoc?

后端 未结 2 1117
难免孤独
难免孤独 2020-12-12 23:45

Having next example:

var CONF = {
    locale: {
        \"en\": {
            name: \"English\",
            lang: \"e         


        
相关标签:
2条回答
  • 2020-12-12 23:49

    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
       }} */
    
    0 讨论(0)
  • 2020-12-13 00:07

    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"
            }
        }
    };
    
    0 讨论(0)
提交回复
热议问题