4

I want to document a type that is defined in a node module (discord.js in my case) so that VS Code can help me with autocompletion.

VS Code supports type hints with JSDoc and this should be the correct way to document the type of the parameter client. Yet VS Code, when I hover over the parameter, says that it is of type any.

(parameter) client: any

This is my code so far

module.exports = class Receiver {
    /**
     * @param {module:"discord.js".Client} client
     */
    constructor(client) {
        this.client = client
    }
}

How can I make VS Code understand the correct type of the parameter? It should not be any but Client instead.

PS: I have installed the discord.js module and can successfully use it.

4
  • If you require('discord.js') and assign that to a variable, say discord, then you should be able to use @param {discord.Client} client to get your type hinting. Commented Aug 22, 2019 at 0:53
  • This does work but I'd rather not have to require('discord.js') because of the overhead and I would not use the variable discord anywhere in my code. (discord would be an unused variable) Commented Aug 22, 2019 at 12:29
  • If you're using Closure Compiler than you'd want to define an extern for discord.js. With regard to defining but not using require('discord.js); I agree this is the right solution, and that you should use some sort of tree shaking, whether it be Closure Compiler's "Advanced Optimizations" or Rollup, or a WebPack config, to avoid the file being bundled. Commented Aug 22, 2019 at 15:22
  • 1
    I think I can get this work this way, however I am still interested if there is a "better" solution that does not require require('discord.js') Commented Aug 22, 2019 at 20:53

1 Answer 1

1

It's not official jsdoc.app syntax, but it works for IntelliSense/TypeScript:

module.exports = class Receiver {
    /**
     * @param {import("./discord.js").Client} client
     */
    constructor(client) {
        this.client = client
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.