模块:插件

例如,当你要处理扩展了另一个库的 JavaScript 代码时。

ts
import { greeter } from "super-greeter";
// Normal Greeter API
greeter(2);
greeter("Hello world");
// Now we extend the object with a new function at runtime
import "hyper-super-greeter";
greeter.hyperGreet();

“super-greeter”的定义:

ts
/*~ This example shows how to have multiple overloads for your function */
export interface GreeterFunction {
(name: string): void
(time: number): void
}
/*~ This example shows how to export a function specified by an interface */
export const greeter: GreeterFunction;

我们可以像下面这样扩展现有模块:

ts
// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>
/*~ This is the module plugin template file. You should rename it to index.d.ts
*~ and place it in a folder with the same name as the module.
*~ For example, if you were writing a file for "super-greeter", this
*~ file should be 'super-greeter/index.d.ts'
*/
/*~ On this line, import the module which this module adds to */
import { greeter } from "super-greeter";
/*~ Here, declare the same module as the one you imported above
*~ then we expand the existing declaration of the greeter function
*/
export module "super-greeter" {
export interface GreeterFunction {
/** Greets even better! */
hyperGreet(): void;
}
}

这里用到了声明合并。

ES6 对模块插件的影响

一些插件会在现有模块上添加或修改顶层导出。虽然这在 CommonJS 和其他加载器中是合法的,但 ES6 模块被视为不可变的,因此这种模式将无法使用。由于 TypeScript 与加载器无关,这一策略在编译时并不会被强制执行,但打算迁移到 ES6 模块加载器的开发者应当注意这一点。

TypeScript 文档是一个开源项目。欢迎发送 Pull Request 帮助我们改进这些页面 ❤

最后更新:2026 年 10 月 23 日