Ich versuche, eine Typdefinition für ein Modul zu erstellen, das module.exports durch eine anonyme Funktion ersetzt. So funktioniert das Modul Code folgendermaßen aus:Wie erstelle ich Typescript (1.8) Type Definition für ein Modul, das das "exports" -Objekt ersetzt?
module.exports = function(foo) { /* some code */}
Um das Modul in JavaScript (Node) verwenden wir dies tun:
const theModule = require("theModule");
theModule("foo");
ich eine .d.ts Datei geschrieben haben, das dies tut:
export function theModule(foo: string): string;
ich dann eine Typoskript-Datei wie folgt schreiben:
import {theModule} from "theModule";
theModule("foo");
Wenn ich in JavaScript transpile, erhalte ich:
const theModule_1 = require("theModule");
theModule_1.theModule("foo");
Ich bin nicht der Modul Autor. So kann ich den Modulcode nicht ändern.
Wie schreibe ich meine Typdefinition, so dass es transpiles richtig:
const theModule = require("theModule");
theModule("foo");
EDIT: Aus Gründen der Klarheit, basierend auf der richtigen Antwort, meine letzte Code sieht wie folgt aus:
die-module.d.ts
declare module "theModule" {
function main(foo: string): string;
export = main;
}
the-Modul-test.ts
import theModule = require("theModule");
theModule("foo");
, die the-Modul-test.js
const theModule = require("theModule");
theModule("foo");