I am currently facing an issue with ES6 import syntax when importing a third-party ES5 module that only exports a single unnamed function:
module.exports = function (phrase, inject, callback) { ... }
Since there is no default export and just an anonymous function output, my import and usage look like this:
import * as sentiment from 'sentiment';
const analysis = sentiment(content);
This results in a Typescript error:
error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "sentiment"' has no compatible call signatures.
It appears that the error stems from incorrectly typing the ES5 import (no public typings file available). Initially, I had created the following definition assuming it was a default export:
interface IResults {
Score: number;
Comparitive: number;
}
declare var fn: (contents: string, overRide?: IDictionary<number>) => IResults;
declare module "sentiment" {
export default fn;
};
However, since the import is not a default export, I am unsure of how to properly define the module and function. I attempted the following:
declare module "sentiment" {
export function (contents: string, overRide?: IDictionary<number>): IResults;
};
Although this seems to be a valid export definition, it does not align with the anonymous call definition and triggers the following error:
error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "sentiment"' has no compatible call signatures.