Discord.js Guide : Property 'commands' does not exist on type 'Client<boolean>' in Typescript

Viewed 2777

node:v16.11.0 "discord.js": "^13.2.0"

I am setting up an example Discord.js bot.

In this guide, I am to the point of adding this line:

client.commands = new Collection();

Typescript then complains with: Property 'commands' does not exist on type 'Client<boolean>'

This answer seems to be in the same vein as what I want. However I'm still running into some problems. If I add a declare module "discord.js" and declare new typings, it doesn't seem to extend the existing type, it overwrites it. And when when I do, it doesn't know what a Collection is in my custom typings file, since that's custom to the new Discord.js library. I can't help but feel this shouldn't be required in an official guide.

I'm wondering if anyone else has made a typescript Discord bot and found a newer solution to this problem.

Thank you for your time.

2 Answers

Okay. So I figured it out.

Create types/common/discord.d.ts

And don't do what I did and just include this:

declare module "discord.js" {
  export interface Client {
    commands: Collection<unknown, any>
  }
}

Make sure this is at the top.

import { Collection } from "discord.js";

Then it will actually extend the existing one and bring in Collection for the definition instead of replacing it.

This solution also required "@typescript-eslint/no-explicit-any": "off" to be added to es-lint, thanks to Collection extending Map and taking any value.

I know it's already answered but for those who doesn't want to import stuff just do this:

module "discord.js" {
    export interface Client {
        foo: number;
    }
}

export {}
Related