如何增加在 Typescript 模块上声明但未导出的类

JCM*_*JCM 2 javascript types typescript

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;
Run Code Online (Sandbox Code Playgroud)

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a6b283d1d43d8c7c82a4a4e22d0fc27c9964154c/types/hubot/index.d.ts#L18-L22

我想Message从我的代码中增加这个类,在hubot.d.ts我编写的文件中:

import * as hubot from 'hubot';

declare namespace Hubot {
  export class Message {
    mentions: string[]
  }
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用: 在此处输入图片说明

hubot.d.ts文件包含在代码中

  "files": ["types/custom/hubot.d.ts"]
Run Code Online (Sandbox Code Playgroud)

tsconfig.json文件中。

我缺少什么?有没有办法做到这一点?

Mat*_*hen 5

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[]
  }
}
Run Code Online (Sandbox Code Playgroud)

需要一个模块扩充来向hubot模块添加声明。由于Hubot命名空间被分配为模块的导出,因此对模块所做的任何扩充都将直接针对该命名空间;namespace Hubot { ... }在扩充中编写另一个会创建一个嵌套的名称空间,这不是您想要的。最后,声明一个类会产生“重复标识符”错误,而声明一个接口就足以添加属性。