Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Typescript模块上扩充已声明但未导出的类_Javascript_Typescript_Types - Fatal编程技术网

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{…}
将创建一个嵌套的名称空间,这不是您想要的。最后,声明一个类会出现“重复标识符”错误,而声明一个接口就足以添加属性。

谢谢,工作得很好,谢谢您的解释。现在有道理了。