Модуль: Плагин
Например, когда вы хотите работать с кодом JavaScript, который расширяет другую библиотеку.
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»:
/*~ 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; Мы можем расширить существующий модуль следующим образом:
// 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-модулей, должны об этом знать.
© 2012-2024 Microsoft
Licensed under the Apache License, Version 2.0.
https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-plugin-d-ts.html