patrick3853
patrick3853

Reputation: 1130

JSDoc reference object variable in @type

Let's say I have the following JS:

let myObj = {
    foo: function(a, b) {},
    bar: function(a) {}
};

Can I reference myObj as a type in JSDoc:

/*
 * @param {myObj} param
 */
function (param) {}

Reading the JSDoc @type documentation, it says a symbol name can be used, but I'm not sure exactly what counts as a "symbol". All the examples I've seen and other questions on stackoverflow show a class name being used, but I'm not defining a class in this case.

For a little more context, I have been defining objects such as this above example using a @typedef and manually listing all the properties. However, this is tedious and feels like code duplication. I recently discovered that PhpStorm still recognizes the return type and has code hints without the @typedef tags, but I'm unsure if this is part of JSDoc or just PhpStorm is smart enough to figure it out.

Upvotes: 1

Views: 2470

Answers (1)

Jack Yu
Jack Yu

Reputation: 2425

You could add @namespace in front of your myObj like following example.
Then, you could use obj name as type to pass it to other type definition.
Note: this is vscode.

enter image description here

enter image description here

But, the obj name must be the same.
Otherwise, it'll not work.

enter image description here

Upvotes: 2

Related Questions