NoNameProvided
NoNameProvided

Reputation: 8987

How document variable in JSDoc when it is an array containing objects?

I have to following data structure:

this.subscribers = [
  {
    callback: () => {console.log( 'my callback'); },
    interval: 5,
    remaining: 3
  },
  {
    callback: () => {console.log( 'my callback 2'); },
    interval: 20,
    remaining: 1
  }
];

I have wrote the JSDoc like this:

/**
 * This variable store the array of callbacks to call repeatedly with the specified interval.
 * @property {function} callback  - The callback function the SchedulerService call.
 * @property {number}   interval  - The number of minutes between the runs.
 * @property {number}   remaining - The remaining minutes until the next call.
 * @type {Array}
 */
this.subscribers = [];

But with this jsdoc the subscriptions variable should look like this:

this.subscribers = [];
this.subscribers.callback = () => {};
this.subscribers.interval = 10;
this.subscribers.remaining = 2;

How does the proper JSDoc comment looks like for this property?

Note: We are talking here about @property and not @param.

Upvotes: 3

Views: 539

Answers (1)

Sebastien Daniel
Sebastien Daniel

Reputation: 4778

{[].<TypeOfObject>}

or

{Array.<TypeOfObject>}

I find it a little awkward, but you can see an example in the docs for the row:

Arrays and objects (type applications and record types)

In your case, you would first define the Subscriber type:

/**
 * @typedef {Object} Subscriber
 * @property {function} callback  - The callback function the SchedulerService call.
 * @property {number}   interval  - The number of minutes between the runs.
 * @property {number}   remaining - The remaining minutes until the next call.
 */

And then you would reference the type in your constructor (whatever it is) as such:

/**
 * This variable store the array of callbacks to call repeatedly with the specified interval.
 * @type {Array.<Subscriber>}
 */
this.subscribers = [];

Upvotes: 1

Related Questions