Discord.js Intellisense not working on sub file

Viewed 531

I have the client function in one file, and after the client receives the message, it sends the message to another function which is on another file(sub file). But there is no autocomplete or intellisense on sub file. Here is my code

//main file
const Discord = require('discord.js')
const client = new Discord.Client();
const subCmd = require('./subCmd.js')
client.on('message', (msg) => {
  subCmd.exec(msg);
}

//subfile
module.exports = {
  exec(msg){
    //no intellisense for msg :(
  }
}

2 Answers

As HippoBaguette said, switching to Typescript is a good option. However, it's a totally new language, and will take a lot of adjustment to convert your codebase. If you want a fix that you can use in JavaScript, then use JSDoc!

This is a service used to document your code, and everything you write in JSDoc will help Intellisense infer what you're trying to do, and how it can help. For starters, to add Intellisense to your function, you can do this:

module.exports = {
  /**
   * @param {Discord.Message} msg
   */
  exec(msg){
    //intellisense for msg :)
  }
}

There's no intellisense because it has a type of any.

You can get intellisense by making it a .ts file and assign the argument msg a type of Message

import { Message } from "discord.js";

module.exports = {
  exec(msg: Message){
    //code
  }
};

You will need @types/node and typescript installed if you haven't already

Related