javascript - 無法調用其類型缺少調用簽名的表達式。類型 'typeof ""' 沒有兼容的調用簽名 React TypeScript
我正在嘗試將三個腳本從 Javascript 轉換為 TypeScript。這些腳本可以在這個 gist 中找到。不過,我還有一個無法解決的錯誤。請注意,這是我的第一個 React 和 TypeScript 專案,所以我很可能錯過了一些明顯的東西。我正在使用 .tsx 文件和 TypeScript 2.1。
在檔案中: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
看起來你的匯入不正確 :)
請嘗試像這樣導入它 import cache from './ScriptCache'。如果你只是使用 gist 中的代碼,cache 也作為默認導出。
如果這不起作用,嘗試 import { ScriptCache as cache } from './ScriptCache'。你不需要使用 as 語法。這只是為了讓你不用更改變數名稱。
答案#2
結果如 @Sebastian Sebald 所建議,是一個導入錯誤。
錯誤 :
import * as cache from './ScriptCache'
正確:
import cache from './ScriptCache'
