2017-01-01 99 views
1

我與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.

回答

1

您將無法在這種情況下導入這種方式。
因爲它在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; 
+0

謝謝,這確實訣竅。 – ken