JSDoc - Documentation for object property that is defined later

Viewed 78

How to document an object property/method, when this property/method gets added to the object later, after the object has been initialized?

Example:

Let's say that I declare the following object. Documentation of ID and name work fine:

let AppUser = {
   /** The user's id */
   ID: 0,
   /** The user's name */
   name: "guest",
};

Later, I decided to add a property (let's say in another js file):

/** The role of the AppUser */
AppUser.role = "admin";

In that case, App.role's documentation does not work. At least, VSCode does not open the tooltip to inform me about its description.

What I do now, is declare an empty property inside the object: role:null, and also write its documentation. This workaround works OK.

1 Answers

To make it work the object definition should be a bit different. Something like the following:

a.js

const AppUser = {}

AppUser.ID = 0
AppUser.name = 'guest'

b.js

AppUser.role = 'admin'

c.js

Screenshot

Related