如何为替换“exports”对象的模块创建Typescript(1.8)类型定义?
问题描述:
我正在尝试为使用匿名函数替换module.exports的模块创建一个类型定义。因此,模块代码做到这一点:如何为替换“exports”对象的模块创建Typescript(1.8)类型定义?
module.exports = function(foo) { /* some code */}
在JavaScript中使用(节点)的模块,我们这样做:
const theModule = require("theModule");
theModule("foo");
我写了一个.d.ts文件,这是否:
export function theModule(foo: string): string;
然后我就可以写一个打字稿文件是这样的:
import {theModule} from "theModule";
theModule("foo");
当我编译成JavaScript时,得到:
const theModule_1 = require("theModule");
theModule_1.theModule("foo");
我不是模块作者。所以,我不能更改模块代码。
我怎样写我喜欢的类型定义,以便正确transpiles到:
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");
这将transpile到的模块-test.js
const theModule = require("theModule");
theModule("foo");