【问题标题】:Discord.js Guide : Property 'commands' does not exist on type 'Client<boolean>' in TypescriptDiscord.js 指南:Typescript 中的“Client<boolean>”类型上不存在属性“commands”
【发布时间】:2021-11-28 16:34:03
【问题描述】:

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

我正在设置一个示例 Discord.js 机器人。

this guide,我要添加这一行:

client.commands = new Collection();

Typescript 然后抱怨:Property 'commands' does not exist on type 'Client&lt;boolean&gt;'

This answer 似乎和我想要的一样。但是我仍然遇到一些问题。如果我添加 declare module "discord.js" 并声明新类型,它似乎并没有扩展现有类型,而是覆盖它。当我这样做时,它不知道我的自定义类型文件中的 Collection 是什么,因为这是新的 Discord.js 库的自定义。我不禁觉得官方指南中不应该要求这样做。

我想知道是否有人制作了打字稿 Discord 机器人并找到了解决此问题的新方法。

感谢您的宝贵时间。

【问题讨论】:

  • 我还没有尝试过,但是:尝试创建一个新类 extends discord.js 的 Client 并添加您需要的任何条目。打字应该被带到新的班级

标签: typescript discord discord.js


【解决方案1】:

好的。所以我想通了。

创建types/common/discord.d.ts

不要像我做的那样,只包括这个:

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

确保它位于顶部。

import { Collection } from "discord.js";

然后它实际上会扩展现有的,并为定义引入Collection,而不是替换它。

此解决方案还需要将 "@typescript-eslint/no-explicit-any": "off" 添加到 es-lint,这要感谢 Collection 扩展 Map 并获取任何值。

【讨论】:

  • 对我不起作用。 import { Collection } 行现在会引发以下错误 Circular definition of import alias 'Collection'.ts(2303)Module '"discord.js"' declares 'Collection' locally, but it is not exported.ts(2459)
猜你喜欢
  • 2017-10-17
  • 2023-04-02
  • 2018-08-17
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 2018-01-14
相关资源
最近更新 更多