モジュール: Plugin

たとえば、別のライブラリを拡張する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ドキュメントはオープンソースプロジェクトです。プルリクエストを送信して、これらのページの改善にご協力ください by sending a Pull Request

このページへの貢献者
MHMohamed Hegazy (53)
OTOrta Therox (14)
BB2D1 (1)
1+

最終更新日: 2024年3月21日