Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document a dictionary in JSDoc?

Having next example:

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

And knowing that what the locale property contains is a dictionary object, which comes from the database, how can I document its inner properties with JSDoc?

Currently I am thinking to typedef type for my locale objects, then may I be able to set the locale property to simply an Array of my defined type? Is this the right way to do it?

like image 411
Áxel Costas Pena Avatar asked Oct 22 '13 09:10

Áxel Costas Pena


People also ask

What is Typedef in JSDoc?

The @typedef tag is useful for documenting custom types, particularly if you wish to refer to them repeatedly. These types can then be used within other tags expecting a type, such as @type or @param. Use the @callback tag to document the type of callback functions.

What are JSDoc comments?

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.

What is JSDoc in Nodejs?

JSDoc is an open source API documentation generator for Javascript. It allows developers to document their code through comments.


2 Answers

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"
        }
    }
};
like image 171
Áxel Costas Pena Avatar answered Oct 23 '22 06:10

Áxel Costas Pena


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
   }} */
like image 34
Daniel Winterstein Avatar answered Oct 23 '22 07:10

Daniel Winterstein