node.js - 从外部 3rd 方打字稿模块扩展类
问题描述
你好我有覆盖类型的问题
我想从将属性添加到其他库的类型的库中覆盖一个类型,该行是:https ://github.com/discord-akairo/discord-akairo/blob/e092ce4e0c9e749418601476bcd054a30a262785/src/index.d.ts# L14
在我的代码中我这样声明:
declare module 'discord.js' {
export interface Message {
util?: KopekUtil;
}
}
KopekUtil 正在扩展 CommandUtil,我得到的错误是:
TS2717: Subsequent property declarations must have the same type. Property 'util' must be of type 'CommandUtil', but here has type 'KopekUtil'. index.d.ts(16, 13): 'util' was also declared here.
解决方案
您提到尝试像这样扩展 Command util 类
export class KopekUtil extends CommandUtil{
constructor(handler, message: Message | CommandInteraction) {
super(handler, <Message>message);
}
send(options:string | MessageOptions , reply? : boolean){
//your logic
}
}
但恐怕不可能覆盖来自外部打字稿模块的类。尽管您可以从外部模块在类中引入新方法或使用对象原型扩展现有方法之一。
正确的方法
实用程序.js
declare module 'discord-akairo'{
export interface CommandUtil {
mySendMethod(options:string | MessageOptions , reply?:any) : boolean
}
};
CommandUtil.prototype.mySendMethod = function (options:string | MessageOptions , reply?:any) : boolean{
return true;
}
打字稿现在合并接口,你可以使用你的扩展
const message = new Message(new Client(),{},new TextChannel(new Guild(new Client(),{})))
message.util.mySendMethod("hello")
推荐阅读
- go - Vulkan VK_ERROR_LAYER_NOT_PRESENT
- spring-boot - 使用 MySQL 数据库进行 Springboot、Maven、单元和集成测试
- android - 清单合并失败并出现错误,AndroidManifest.xml 中的属性 provider#androidx.core.content.FileProvider@authorities 需要占位符
- javascript - expo sdk 40 metro.config.js “预期‘fromDir’为‘字符串’,得到‘未定义’”
- javascript - 基于时间的 (performance.now()) 函数输出的 Javascript 不一致
- animation - 动画算法
- r - shinysurveys 结果到 csv
- swiftui - 从孩子的根视图顶部覆盖视图?
- c# - 有没有办法让布局初始化(异步)在页面主体初始化之前出现?
- python - Python excel to pdf不会打印所有工作表