Javascript 如何在Typescript模块上扩充已声明但未导出的类
Javascript 如何在Typescript模块上扩充已声明但未导出的类,javascript,typescript,types,Javascript,Typescript,Types,hubot类型定义具有以下类别: declare namespace Hubot { // ... class Message { user: User; text: string; id: string; } // ... } // Compatibility with CommonJS syntax exported by Hubot's CoffeeScript. // tslint:disable-ne
hubot
类型定义具有以下类别:
declare namespace Hubot {
// ...
class Message {
user: User;
text: string;
id: string;
}
// ...
}
// Compatibility with CommonJS syntax exported by Hubot's CoffeeScript.
// tslint:disable-next-line export-just-namespace
export = Hubot;
export as namespace Hubot;
我想从我编写的hubot.d.ts
文件中的代码中扩充Message
类:
import * as hubot from 'hubot';
declare namespace Hubot {
export class Message {
mentions: string[]
}
}
但它不起作用:
hubot.d.ts
文件通过
"files": ["types/custom/hubot.d.ts"]
在tsconfig.json
文件中
我错过了什么?有什么方法可以做到这一点吗?
hubot.d.ts
应该包含:
// This import serves no purpose except to make the file a module
// (so that the following statement is a module augmentation rather
// than a module declaration) and could be replaced with `export {}`.
import * as hubot from 'hubot';
declare module "hubot" {
interface Message {
mentions: string[]
}
}
将声明添加到
hubot
模块中需要一个。由于Hubot
名称空间被指定为模块的导出,因此对模块进行的任何扩展都将直接针对该名称空间;在扩充中编写另一个名称空间Hubot{…}
将创建一个嵌套的名称空间,这不是您想要的。最后,声明一个类会出现“重复标识符”错误,而声明一个接口就足以添加属性。谢谢,工作得很好,谢谢您的解释。现在有道理了。