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

前端之家收集整理的这篇文章主要介绍了如何导入在Typescript中使用module.exports =的CommonJS模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_1@以下产生有效的工作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模块中的文字值,但导出默认值表示您正在导出默认属性,这不是您的JavaScript代码实际执行的操作.

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

declare module "test" {
    function myfunc(): string;
    export = myfunc;
}
原文链接:https://www.f2er.com/js/240766.html

猜你在找的JavaScript相关文章