-1

I know that I can use jsdoc to declare that a function returns an Object.

/**
 *  Builds a body that returns published products
 * @param {string} rangeType
 * @param {string} retailUnit
 * @param {string[]} ids
 * @returns {Object}
 */
function fetchPublishedProducts(rangeType, retailUnit, ids) {
  return {
    size: 250,
    query: {
      ids: {
        values: ids.map(id => `${id}-${retailUnit}${rangeType ? `_${rangeType}` : ''}`),
      },
    },
  };
}

But what is the syntax if I want to declare that the return object has a size and a query property?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user1283776
  • 19,640
  • 49
  • 136
  • 276
  • Does this answer your question? [How to describe "object" arguments in jsdoc?](https://stackoverflow.com/questions/6460604/how-to-describe-object-arguments-in-jsdoc) – jonrsharpe Nov 11 '20 at 15:02

2 Answers2

3

Just define your object interface as:

/**
 * ....
 * @returns {{size: number, query: string}}
 */
Raeisi
  • 1,647
  • 1
  • 6
  • 18
0

You can use object interface as:

/**
 * @returns {{a: number, b: string}}
*/
function foo() {
  return {
    a: 200,
    b: 'Hello world'     
  }
}

or (option b)

You can use a @typedef annotation.

First define your structure separately:

/**
 * @typedef {Object} myObj
 * @property {number} a - some comment
 * @property {string} b - name of product
 */

and use the return type:

/**
 * 
 * @returns {myObj}
 */
function foo(){
  return {
    a: 200,
    b: 'Hello world'
  }
}