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

Below is an example of a valid ES5, but it emits an error below. I am using Typescript 1.7.5 and I think I read the entire language specification and I cannot understand why this error occurs.

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

a.js (external ES5 module with default export)

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

adts

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

b.ts

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

b.js (generated ES5):

 var test = require("test"); var app = test() 
+5
source share
1 answer

module.exports exports a literal value in a CommonJS module, but export default says that you are exporting a default property that is not your JavaScript code.

The correct export syntax in this case is simply export = myfunc :

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

Source: https://habr.com/ru/post/1243004/


All Articles