如何在 Typescript 中导入使用 module.exports= 的 CommonJS 模块

How to import CommonJS module that uses module.exports= in Typescript

以下生成有效的 ES5,但发出以下错误。我正在使用 Typescript 1.7.5,我想我已经阅读了整个语言规范,但我无法弄清楚为什么会产生此错误。

error TS2349: Cannot invoke an expression whose type lacks a call signature.

a.js(默认导出的 ES5 环境模块)

function myfunc() {
  return "hello";
}
module.exports = myfunc;

a.d.ts

declare module "test" {
    export default function (): string;
}

b.ts

import test = require("test");
const app = test();

b.js(生成 ES5):

var test = require("test");
var app = test()

module.exports 在 CommonJS 模块中导出文字值,但 export default 表示您正在导出 default 属性,这不是您的 JavaScript 代码确实如此。

在这种情况下正确的导出语法只是 export = myfunc:

declare module "test" {
    function myfunc(): string;
    export = myfunc;
}