2016-05-14 81 views
2

我正在嘗試爲使用匿名函數替換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"); 

回答

1

對於導出函數節點風格模塊,use export =

function theModule(foo: string): string; 
export = theModule; 
相關問題