physicsboy
physicsboy

Reputation: 6326

Using supertest in TypeScript - Getting problem "cannot invoke expression whose type lacks a call signature"

I am trying to use supertest with TypeScript after initially using with JavaScript.

However I am now receiving red squiggles under my code stating:

cannot invoke expression whose type lacks a call signature. Type 'typeof supertest' has no compatible call signatures

I am using supertest in the following way:

import * as supertest from 'supertest';

const request = superTest(some.url);

// Forgive my pseudocode
function makeRequest() {
  return request.get('/endpoint')...
}

Is this something I should be worried about? How can I correct this?

Upvotes: 6

Views: 6085

Answers (2)

Sufian
Sufian

Reputation: 6555

I had the @types/supertest added (as suggested by the other answer) but doing the following solved the problem:

  1. import supertest like this:

    import supertest from 'supertest';
    
  2. in my index.ts file, export the value of app.listen(...); like following (previously it was export default server):

    export {server};
    

Upvotes: 15

Dániel Kis-Nagy
Dániel Kis-Nagy

Reputation: 2505

When you switch to TypeScript, you need to provide type information for the external libraries you use. Many libraries do this for you "out of the box", while for others, you have to install the type information files yourself.

In case of supertest, installing this NPM package should solve your problem: https://www.npmjs.com/package/@types/supertest

Upvotes: 4

Related Questions