Search code examples
node.jsvisual-studio-codejsdoc

How to use type from other module


JSDoc under VS Code: How to use type from other module? In the example below, how to declare foo(ta)?

ma.js (CommonJS)

class TA {...}

module.exports = { makeTA: () => new TA, };

mb.js (CommonJS)

const ma = require('./ma');

foo(ma.makeTA());

/** @param {???} ta */ // want to use TA from ma
function foo(ta) {...}

Solution

  • You can use import("./ma") as the type. But you also have to export the class you want to use as the type.

    ma.js

    class TA { }
    
    module.exports = { makeTA: () => new TA, TA };

    mb.js

    /** @param {import("./ma").TA} ta */
    function foo(ta) {...}

    Like this "ta" will be typed as the instance of the class from the other file.