使用node-fetch时,无法调用其类型缺少调用签名的表达式

时间:2017-04-30 16:12:07

标签: node.js typescript fetch-api

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

1 个答案:

答案 0 :(得分:16)

您已导入整个模块,而不是默认导出,即获取功能。您试图将整个模块称为无法正常工作的功能。

而不是

import * as fetch from 'node-fetch';

import fetch from 'node-fetch';