我已经下载了模块的类型定义,假设 modulea ,来自 @types/module-a
。
模块-A.D.TS 文件看起来像
declare module "module-a" {
export = moda;
}
declare namespace moda {
interface MODAPromise<T> {
isResolved(): boolean;
....;
}
}
现在,在我的应用程序中,我发现我需要使用一些其他规格扩展这些类型。
遵循较早收到的建议,我在我的src目录中构建一个文件 module-a.augmented.d.t.ts sike y this
declare module "module-a" {
export interface MODAPromise {
myNewProperty: any;
}
}
如果我这样做,那么Typescript信号"不能在模块中使用其他导出元素的导出分配。" 。有没有办法扩展此类声明,而无需触摸原始模块-A.D.TS file?
模块-A.D.TS 的export = moda;
我发现我可以将export =
语法与namespace
结合起来,以从接口导出类型。export =
是必要的(据我所知(,以表明外部模块使用COMPONJS风格的exports
而不是ES6导出。如果您尝试在同一模块中同时使用export =
和export
,则将收到以下错误:
TS2309:不能在模块中使用其他导出元素的导出分配。
但是,如果您声明一个声明一个名称空间,其名称与exports =
表达式中使用的变量相同,则可以将类型放置在该名称空间的内部,使其可用于消耗模块。
这是使用此技术的模块类型定义的示例:
declare module 'some-module' {
namespace SomeClass {
export interface IMyInterface {
x:string;
};
}
class SomeClass {
constructor(p:SomeClass.IMyInterface);
}
export = SomeClass;
}
这是因为您将导出设置为" module-a"中的命名空间moda,该命名空间是在module-a.d.ts中定义的,以及 export modapromise在"模块-A"中定义在Module-a.Augmented.d.ts。
中定义因此,您正在尝试定义这样的"模块-A":
declare module "module-a" {
export = moda;
export interface MODAPromise {
// ...
}
}
您正在尝试设置导出,并同时导出其他内容,这是没有意义的。您需要找到另一种方式来导出Moda的Modapromise和增强的Modapromise。