newBee
newBee

Reputation: 1319

JSDoc @param - reference class from another module

I have 2 Node modules. In module A I have the following definition:

/**
 * HTTP Client
 * @module src/http/client
 */
/**
 * A HTTP Client
 * @alias src/http/client
 */
class HTTPClient {
   [... class def with documented methods etc]
}
module.exports = HTTPClient

And now in module B I want to say that the first constructor parameter should be of type HTTPClient. So I tried the following

class PackageFactory {
    /**
     * @param {module:src/http/client} httpClient - the HTTPClient instance
     */
    constructor(httpClient) {
       this._httpClient = httpClient
    }
}

I also tried a few variations but it never worked. Within module B the httpClient is always of type "any". What do I have to change so I can see the class member of HTTPClient in module B?

Upvotes: 4

Views: 2224

Answers (2)

Aurelien Ribon
Aurelien Ribon

Reputation: 7634

To avoid the HTTPClient is unused error that any linter will give you, you can actually import the class in the @param field itself. This makes the doc a bit less easy to read, but you get autocompletion at design time, a nice tradeoff.

Note that you can combine this with the compilerOptions.paths option of tsconfig.json to create aliases that will make those documentation-imports look better.

class PackageFactory {
    /**
     * @param {import('../http/client')} httpClient - the HTTPClient instance that shall be used to make requests
     */
    constructor(httpClient) {
       this._httpClient = httpClient
    }
}

Upvotes: 1

newBee
newBee

Reputation: 1319

The solution was easier then I thought. There is no need to include the module paths (aka longname) or anything.

const HTTPClient = require('../http/client')
class PackageFactory {
    /**
     * @param {HTTPClient} httpClient - the HTTPClient instance that shall be used to make requests
     */
    constructor(httpClient) {
       this._httpClient = httpClient
    }
}

Upvotes: 4

Related Questions