如何为代替“exports”对象的模块创buildTypescript(1.8)types定义?
我想创build一个模块的types定义,用一个匿名函数replacemodule.exports。 所以,模块代码这样做:
module.exports = function(foo) { /* some code */}
要在JavaScript(Node)中使用模块,我们这样做:
const theModule = require("theModule"); theModule("foo");
我已经写了一个.d.ts文件这样做:
export function theModule(foo: string): string;
然后我可以像这样写一个TypeScript文件:
import {theModule} from "theModule"; theModule("foo");
当我编译成JavaScript时,我得到:
const theModule_1 = require("theModule"); theModule_1.theModule("foo");
我不是模块作者。 所以,我不能改变模块代码。
如何编写我的types定义,以便它正确地转换为:
const theModule = require("theModule"); theModule("foo");
编辑:为了清晰起见,基于正确的答案,我的最终代码如下所示:
该-module.d.ts
declare module "theModule" { function main(foo: string): string; export = main; }
所述模块-test.ts
import theModule = require("theModule"); theModule("foo");
这将传递到模块test.js
const theModule = require("theModule"); theModule("foo");
对于导出函数的节点式模块, 使用export =
function theModule(foo: string): string; export = theModule;