Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc in VS code for documenting function with module's type

I want to document a function written in another module, which uses http.ClientRequest typed parameter. I want something like this, but it does not work:

/**
* @param {ClientRequest} req 
*/

function setToken(req) {
}

I have tried also @param {http.ClientRequest}, but it did not work.

Update: Basically I solved that problem by importing the required module by import http from "http";. However, I don't want to import it because this module does not use http module but provides helper functions.

like image 422
Nursultan Zarlyk Avatar asked Jul 17 '17 10:07

Nursultan Zarlyk


People also ask

Does JSDoc work with TypeScript?

You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.

What is JSDoc used for?

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.

What does @module mean js?

The @module tag marks the current file as being its own module. All symbols in the file are assumed to be members of the module unless documented otherwise. Link to a module (e.g. within a @link or @see tag) using "module:moduleName". For example, "@module foo/bar" can be linked to using "{@link module:foo/bar}".


2 Answers

After improve answer from IGx89 I got a shorter variant without typedef. I prefer this variant when I reference to another module one time:

/**
* @param {import('http').ClientRequest} req 
*/
function setToken(req) {

}

But if you need reference to some type from another module with long path variant with typedef looks shorter.

like image 136
DI_DJON Avatar answered Nov 15 '22 04:11

DI_DJON


Add the following to the top of your file:

/** @typedef {import('http').ClientRequest} ClientRequest */

I had this same problem myself (though regarding a module in my own app) and also had a very hard time finding the solution. I eventually figured out the above syntax by reading through this issue in the TypeScript GitHub repo: https://github.com/Microsoft/TypeScript/issues/14377

like image 35
IGx89 Avatar answered Nov 15 '22 04:11

IGx89