【问题标题】:How to write a module declaration that augments another module declaration如何编写扩展另一个模块声明的模块声明
【发布时间】:2017-06-28 21:16:37
【问题描述】:

我正在尝试为一些用 JavaScript 编写的现有 AMD 模块编写 TypeScript 声明。

我试图表示的结构是一个“插件”模型,其中一个模块声明一个接口,另一个模块用“插件”扩展该接口。我正在尝试遵循模块扩充here 的示例,但这似乎不允许我创建两个单独的模块。

例子:

  • 模块“foo”定义了 Foo 接口并导出该接口的单例常量
  • 模块“foo-plugin”为 Foo 接口添加了一个新的“插件”功能

从消费者方面,我希望行为如下:

// 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();

这种行为可能吗?声明会是什么样子?

【问题讨论】:

    标签: typescript amd


    【解决方案1】:

    我有 the same problem 和 fastify 插件声明。

    declare module "foo-plugin" {
    
        module "foo" {
            function plugin(): void;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      相关资源
      最近更新 更多