How to use type from other module

22 Views Asked by At

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) {...}
1

There are 1 best solutions below

2
Lucasion On BEST ANSWER

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.