2017-04-25 240 views
0

我想从JavaScript转换三个脚本到TypeScript。脚本可以在这个gist中找到。然而,我确实有一个我无法摆脱的错误。请注意,这是我的第一个React和TypeScript项目,所以我很容易就错过了一些明显的东西。我正在使用.tsx文件和TypeScript 2.1。无法调用其类型缺少呼叫签名的表达式。键入'typeof'“''没有兼容的呼叫签名反应TypeScript

在文件:GoogleApiComponent.tsx

import * as cache from './ScriptCache' 
... 
componentWillMount() { 
    this.scriptCache = cache({ <--Error TS2349 here 
     google: GoogleApi({ 
      apiKey: apiKey, 
      libraries: libraries 
     }) 
    }); 
} 

给了我这个错误:

Cannot invoke an expression whose type lacks a call signature. Type 'typeof "ScriptCache"' has no compatible call signatures. 

ScriptCache.tsx看起来是这样的:

let counter = 0; 
let scriptMap = new Map(); 

export const ScriptCache = (function (global: any) { 
    return function ScriptCache(scripts: any) { 
     const Cache: any = {} 
     ... 
+1

请举一个完整的例子。这也可能是由于你的模块导入/导出错误发生在哪一行。 –

+0

@SebastianSebald你是对的,这是一个导入错误!非常感谢! – Ogglas

回答

2

看来你输入不正确:)

请尝试导入像这样import cache from './ScriptCache'。如果您只是使用了要点中的代码,则缓存也会默认导出。如果这不起作用,请尝试import { ScriptCache as cache } from './ScriptCache'。您不需要as语法。这只是所以你不必更改变量名称。

+1

哈哈只是自己写了一个答案,但将标记为正确的。再次感谢! – Ogglas

+0

不客气! :) 看到了。有点太晚了! –

1

原来通过的建议是一个导入错误@Sebastian Sebald。

错误:

import * as cache from './ScriptCache' 

正确:

import cache from './ScriptCache' 
+1

如果您想更好地理解模块语法:这可能有所帮助:http://stackoverflow.com/questions/43385452/difference-between-import-as-button-and-import-button/43385719#43385719 :) –

相关问题