Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting typescript function "lacks a call signature"

Tags:

typescript

I have a typescript external module in "main.ts", which only exports a single function, written in this way:

// ...
import O = require('./Options');

"use strict";

function listenRestRoutes(expressApp: any, options?: O.IOptions) {
    // ...
}
module.exports = listenRestRoutes;

This one compiles well. And I have another file, where this module is imported:

// ...
import express = require('express');
import mipod = require('./main');
import O = require('./Options');
// ...
var app = express();
var opts: O.IOptions = O.Options.default();
// ...
mipod(app, opts);

The last line doesn't compile, saying error TS2088: Cannot invoke an expression whose type lacks a call signature. mipod(app, opts);

I don't understand why I get this error. Despite this error, the javascript is correctly generated and runs well. So, is it a compiler bug, or is there something bugged in my code?

PS: I also tried to add reference on top of the second file:

/// <reference path="./main.ts" />

But it doesn't change anything.

like image 423
Joel Avatar asked Mar 16 '26 12:03

Joel


1 Answers

TypeScript doesn't parse module.exports assignments for type information. Instead of this line:

module.exports = listenRestRoutes;

Use this

export = listenRestRoute;
like image 143
Ryan Cavanaugh Avatar answered Mar 19 '26 02:03

Ryan Cavanaugh