使用节点提取时,不能调用其types缺less调用签名的expression式
我试图让我的脚本工程中的node-fetch
工作:
import * as fetch from 'node-fetch'; import * as assert from 'assert'; export class DatabaseConfigurator { private url: string; getNode (): Promise<string> { return fetch(`${this.url}/_membership`).then((response: fetch.Response) => { return response.json(); }).then((res: any) => { assert.equal(res.all_nodes.length, 1); return res.all_nodes[0]; }); } }
我得到:
Cannot invoke an expression whose type lacks a call signature. Type 'typeof "/home/vinz243/compactd/node_modules/@types/node-fetch/index"' has no compatible call signatures.
当我安装的定义似乎确定( node_modules/@types/node-fetch/index.d.ts
):
... export default function fetch(url: string | Request, init?: RequestInit): Promise<Response>;
我的tsconfig.json
{ "compilerOptions": { "sourceMap": true, "outDir": "./dist/", "noImplicitAny": true, "module": "commonjs", "target": "es6", "allowJs": true }, "include": [ "./src/**/*" ] }
您已经导入了整个模块,而不是默认的导出,即获取函数。 你试图把整个模块作为一个不起作用的函数。
代替
import * as fetch from 'node-fetch';
尝试
import fetch from 'node-fetch';