繁体   English   中英

无法调用类型缺少调用签名的表达式。 类型'typeof“”'没有兼容的调用签名React TypeScript

[英]Cannot invoke an expression whose type lacks a call signature. Type 'typeof “”' has no compatible call signatures React TypeScript

我正在尝试将三个脚本从Javascript转换为TypeScript。 可以在本要点中找到这些脚本。 但是,我确实有一个我无法摆脱的错误。 请注意,这是我的第一个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 = {}
        ...

看来您输入的是不正确的:)

请尝试import cache from './ScriptCache'import cache from './ScriptCache'一样导入它。 如果您仅使用了要点中的代码,则缓存也将默认导出。

如果这不起作用,请尝试import { ScriptCache as cache } from './ScriptCache' 您不需要as语法。 只是这样,您不必更改变量名称。

原来是@Sebastian Sebald建议的导入错误。

错误:

import * as cache from './ScriptCache'

正确:

import cache from './ScriptCache'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM