Get Off My Lawn
Get Off My Lawn

Reputation: 36299

Type lacks a call signature

I am not sure what I am missing, but I have a function that looks like this:

export default function (config: DatabaseConnections<DatabaseConnection>) {
  return DB['connect'](config, throwError)
}

When I try to use the function (in my js test case), I would like to use it like this:

const db = require('../lib/DB')
db({
  master: {
    host: 'abc123',
    user: 'abc123',
    password: 'abc123',
    database: 'abc123'
  }
})

However, when I do that I get the error:

Cannot invoke an expression whose type lacks a call signature.

When I call it like this db.default({...}) it works. How can I get it to export the function so I can call it like this db({...})?

Upvotes: 2

Views: 156

Answers (1)

Gopherkhan
Gopherkhan

Reputation: 4342

There are a few ways to fix this

Simple fix

You can try grabbing the default off the require:

const db = require('../lib/DB').default;

Named Function Fixes

You could also try naming the function rather than relying on the default.

In your DB file, try:

export default function db (...)

and then in your test file:

const db = require('../lib/DB').db;

You can also name and export only the specific function, rather than using a default.

A la:

function db(...){ }
module.exports=db;

And then in your test file:

const db = require('../lib/DB');

Bonus Return Type Fix

Since the db function returns a value, you need to define a return type on the function.

export default function (config: DatabaseConnections<DatabaseConnection>):DB {
  return DB['connect'](config, throwError);
}

Upvotes: 1

Related Questions