【问题标题】:Cannot invoke an expression whose type lacks a call signature when using node-fetch使用 node-fetch 时无法调用其类型缺少调用签名的表达式
【发布时间】:2017-09-28 05:48:12
【问题描述】:

我正在尝试让 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/**/*"
  ]
}

【问题讨论】:

    标签: node.js typescript fetch-api


    【解决方案1】:

    您已经导入了整个模块,而不是默认导出的 fetch 函数。您试图将整个模块调用为不起作用的函数。

    代替

    import * as fetch from 'node-fetch';
    

    试试

    import fetch from 'node-fetch';
    

    【讨论】:

    • 那我该如何导入响应定义呢?
    • nvm, fetch, {Response} 是要走的路。谢谢!
    猜你喜欢
    • 2017-12-19
    • 2017-07-14
    • 2020-02-07
    • 1970-01-01
    • 2019-01-05
    • 2018-05-09
    • 2017-09-01
    • 2021-02-06
    • 2017-03-17
    相关资源
    最近更新 更多