我正在尝试为一些用 JavaScript 编写的现有 AMD 模块编写 TypeScript 声明。
我试图表示的结构是一个“插件”模型,其中一个模块声明一个接口(interface),另一个模块使用“插件”扩展该接口(interface)。我正在尝试按照模块扩充的示例 here ,但这似乎不允许我创建两个单独的模块。
例子:
- 模块“foo”定义了 Foo 接口(interface)并导出该接口(interface)的单例常量
- 模块“foo-plugin”向 Foo 接口(interface)添加了一个新的“插件”功能
从消费者的角度来看,我希望行为如下:
// by importing this, I can use the foo singleton as type Foo with only the Foo
// functionality defined in "foo"
import { foo } from "foo";
// by also importing this, I can now use the additional Foo.plugin function defined by "foo-plugin" on the foo singleton
import "foo-plugin";
// this should compile if and only if the "foo-plugin" module is imported
foo.plugin();
这种行为可能吗?声明会是什么样子?
最佳答案
我有 the same problem使用 fastify 插件声明。
declare module "foo-plugin" {
module "foo" {
function plugin(): void;
}
}
关于typescript - 如何编写一个模块声明来扩充另一个模块声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44812670/