Search code examples
javascriptnode.jsjsdoc

How to use JSDoc type hints for NPM/internal modules without importing them?


Usually, when I want to refference a class in JSDoc but do not need to import it, I do this, here SimpleLRU is an actual class I wrote:

/** @typedef {import("../js/SimpleLRU.js").default} SimpleLRU **/

Then I can do this, without actually depending on it:

enter image description here

You can see hints on some methods the class has. What I am doing now is I want to have am express route function in a separate file, due to its length. But I still want hints on the request, response and next arguments that express will pass to it. SO I did this, remember, I don't need express here:

/** @typedef {import("express").default} express **/

/**
 * @param {express.Request} request
 * @param {express.Response} response
 */
function my_router(request, response, next) {

}

export default my_router;

But if I write request. I don't get any hints. If I replace the typedef with actual import, it works immediatelly:

import express from "express"

enter image description here

Same situation if I tried node internal package like fs. So how do I do this for internal packages, the way it works for me for local files?


Solution

  • I have solved it in this way. On typedef I have removed default

    /** @typedef {import("express")} express **/
    

    When I have setted the params on javadocs I have used brackets

    /**
     * @param {express["request"]} request
     * @param {express["response"]} response
     */
    function my_router(request, response, next) {
    
    }
    

    If it doesn't work try installing @types/express too

    Let me know :)