0

In JSDoc, how to actually document an object, not as a param?

example

let object1 = {
  name: '' //string,
  age: '' //number,
};

real example

 /**
   * Update User
   * @param {Object} params
   * @param {String} params.oldName
   * @param {String} params.oldAge
   */
  const onChangeUser = params => {
    ....

    //I want to document this object
    let object1 = {
      // name: '', string,
      // age: '' number,
    };
  };

this object doesn't belongs to any function is just an object inside a function but is not used as a param

Juan David
  • 227
  • 2
  • 9
  • Does this answer your question? [What is the correct JSDoc syntax for a local variable?](https://stackoverflow.com/questions/38708777/what-is-the-correct-jsdoc-syntax-for-a-local-variable) – AamirR Nov 21 '21 at 11:00
  • that is to document a single variable but not an object @AamirR – Juan David Nov 21 '21 at 11:01
  • Does this answer your question? [Best way to document anonymous objects and functions with jsdoc](https://stackoverflow.com/questions/3171454/best-way-to-document-anonymous-objects-and-functions-with-jsdoc) – Karl-Johan Sjögren Nov 21 '21 at 11:16

1 Answers1

1

Complex types can be aliased using a typedef @documentation

/**
 * @typedef PersonProperties
 * @type {object}
 * @property {string} name - Full Name of the Person.
 * @property {number} age  - Age of Person.
 */

Usage:

/** @type {PersonProperties} */
let object1 = {
  name: '',
  age: ''
};
AamirR
  • 11,672
  • 4
  • 59
  • 73
  • thank you!!... but in case don't want to add a separate 'typedef', because maybe the object is not that big to actually separate it as a typedef, is there a way to use it as "inline"? :) – Juan David Nov 21 '21 at 11:24
  • 1
    I have not tested in-line google closure compiler syntax, can you check if this works `/** @type {{name: string, age: number}} */` more on the google closure compiler type-casting here https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler#type-casting – AamirR Nov 21 '21 at 11:39
  • 2
    For readability concern I would document `typedef`s to the top, the usage is pretty much a one-liner. – AamirR Nov 21 '21 at 11:42