typescript 扩展 class,跨文件合并申明被覆盖

新手上路,请多包涵

现在有个需求是扩展第三方组件的 class,添加2个属性和方法,需要用到合并申明。

// 组件本身的申明
declare class WS {
    open: () => boolean;
}

declare namespace WS {
    type Meta = string | string[] | Buffer | Buffer[];
}
export = WS;

然后我需要扩展这个 class WS,给他添加一个 close 方法,
我添加了一个 ws.d.ts 的申明文件

import 'ws'

module 'ws'{
  interface WS {
    close: () => boolean;
  }
}

然后就发现不对,组件在 export 的时候,直接导出了 WS。这个 WS 有2个引用,一个是 class WS,一个是 namespace WS

我如果直接加一个 interface 的话, 会覆盖掉 class 的申明。相当于 class 没有了。

但是我不明白的是,我如果在他原来的申明文件里面加 interface 就可以合并申明。

比如,下面这个就是好的,但是我不能去修改组件本身的申明文件。

// 组件本身的申明
declare class WS {
    open: () => boolean;
}

declare namespace WS {
    type Meta = string | string[] | Buffer | Buffer[];
}

// 我加的申明
declare interface WS {
    close: () => boolean;
}
export = WS;

请问我是遗漏了什么吗?

阅读 2k