的ES5匿名函数
问题描述:
我与ES6进口语法工作和导入第三方ES5模块谁出口一单出口这是一个匿名函数的打字稿进口:的ES5匿名函数
module.exports = function (phrase, inject, callback) { ... }
因为没有默认的出口,而是一个匿名函数输出我必须导入和使用像这样:
import * as sentiment from 'sentiment';
const analysis = sentiment(content);
这给打字稿错误:
error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "sentiment"' has no compatible call signatures.
我想我得到的是因为我没有正确输入ES5导入(没有公共打印文件)。回来时,我虽然功能是默认出口我有如下定义:
interface IResults {
Score: number;
Comparitive: number;
}
declare var fn: (contents: string, overRide?: IDictionary<number>) => IResults;
declare module "sentiment" {
export default fn;
};
这一切都感觉良好,但看到的进口是不默认的导出我不知道如何定义这个模块和功能。我曾尝试以下操作:
declare module "sentiment" {
export function (contents: string, overRide?: IDictionary<number>): IResults;
};
,虽然这似乎是一个有效的导出定义并不匿名呼叫定义相符,并引发以下错误:
error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "sentiment"' has no compatible call signatures.
答
您将无法在这种情况下导入这种方式。
因为它在Modules: export = and import = require()指出:
When importing a module using export =, TypeScript-specific import let = require("module") must be used to import the module.
所以你必须要做到这一点:
import sentiment = require("sentiment");
const analysis = sentiment(content);
定义文件或许应该是这样的:
declare function fn(contents: string, overRide?: IDictionary<number>): IResults;
export = fn;
谢谢,这确实诀窍。 – ken