【问题标题】:Typescript - Cannot find name 'fetch' (universal library)打字稿 - 找不到名称“获取”(通用库)
【发布时间】:2017-12-12 18:18:13
【问题描述】:

我的目标是构建一个带有 AJAX 调用的 Typescript 库(通过使用fetch API),客户端(Webpack/ Browserify)后端开发人员(Node)。

但是,我似乎无法正确编译 fetch


我的第一次尝试是使用isomorphic-fetch@types/isomorphic-fetch。我不确定类型是否完整,但它们没有带来任何全局变量(他们应该带来 fetch,不是吗?)

npm i isomorphic-fetch @types/isomorphic-fetch

index.ts

import 'isomorphic-fetch';
export function execute() {
  return fetch('...') ...
}

tsconfig.json

"compilerOptions": {
  ...
  "lib": ["es2015", "es2016", "es2017"]
}

输出:

node_modules/@types/isomorphic-fetch/index.d.ts(8,30): error TS2304: Cannot find name 'fetch'.
src/index.ts(4,25): error TS2304: Cannot find name 'fetch'.

我需要“dom”吗? 显然,使用 dom 库它确实可以编译并在这两个 ✓ 上运行,但我无法控制它是否真的可以在 Node.js 上运行。我的意思是,无论我是否import 'isomorphic-fetch',它都会编译,但如果我错过它,它将在 Node 上失败,恕不另行通知。

另外,Node 不是“dom”,不管我是否也想支持浏览器。


我的第二次尝试是whatwg-fetch

npm i whatwg-fetch @types/whatwg-fetch

tsconfig.json

"lib": ["es2015", "es2016", "es2017"] // The same.

这个甚至没有通过编译阶段(不管“dom”库):

> tsc --watch

node_modules/@types/whatwg-fetch/index.d.ts(11,27): error TS2304: Cannot find name 'window'.
node_modules/@types/whatwg-fetch/index.d.ts(31,25): error TS2304: Cannot find name 'Blob'.
node_modules/@types/whatwg-fetch/index.d.ts(31,64): error TS2304: Cannot find name 'FormData'.
node_modules/@types/whatwg-fetch/index.d.ts(36,21): error TS2304: Cannot find name 'Blob'.
node_modules/@types/whatwg-fetch/index.d.ts(37,25): error TS2304: Cannot find name 'FormData'.
17:31:50 - Compilation complete. Watching for file changes.

带“dom”:

node_modules/typescript/lib/lib.dom.d.ts(9353,11): error TS2300: Duplicate identifier 'Request'.
node_modules/typescript/lib/lib.dom.d.ts(9370,13): error TS2300: Duplicate identifier 'Request'.
node_modules/typescript/lib/lib.dom.d.ts(9375,11): error TS2300: Duplicate identifier 'Response'.
node_modules/typescript/lib/lib.dom.d.ts(9386,13): error TS2300: Duplicate identifier 'Response'.
node_modules/typescript/lib/lib.dom.d.ts(14940,18): error TS2451: Cannot redeclare block-scoped variable 'fetch'.
node_modules/typescript/lib/lib.dom.d.ts(14945,6): error TS2300: Duplicate identifier 'BodyInit'.
node_modules/typescript/lib/lib.dom.d.ts(14966,6): error TS2300: Duplicate identifier 'HeadersInit'.
node_modules/typescript/lib/lib.dom.d.ts(14976,6): error TS2300: Duplicate identifier 'RequestInfo'.
node_modules/typescript/lib/lib.dom.d.ts(15043,6): error TS2300: Duplicate identifier 'ReferrerPolicy'.
node_modules/typescript/lib/lib.dom.d.ts(15044,6): error TS2300: Duplicate identifier 'RequestCache'.
node_modules/typescript/lib/lib.dom.d.ts(15045,6): error TS2300: Duplicate identifier 'RequestCredentials'.
node_modules/typescript/lib/lib.dom.d.ts(15046,6): error TS2300: Duplicate identifier 'RequestDestination'.
node_modules/typescript/lib/lib.dom.d.ts(15047,6): error TS2300: Duplicate identifier 'RequestMode'.
node_modules/typescript/lib/lib.dom.d.ts(15048,6): error TS2300: Duplicate identifier 'RequestRedirect'.
node_modules/typescript/lib/lib.dom.d.ts(15049,6): error TS2300: Duplicate identifier 'RequestType'.
node_modules/typescript/lib/lib.dom.d.ts(15050,6): error TS2300: Duplicate identifier 'ResponseType'.
...

我也尝试过使用其他类似的库,例如 fetch-ponyfill,但这个库甚至没有可用于 TypeScript 的类型。


我们应该如何在通用应用程序(浏览器 + 节点)上调用 fetch

谢谢!

【问题讨论】:

标签: javascript node.js typescript fetch-api isomorphic-fetch-api


【解决方案1】:

如果您使用以下命令进行编译:

"ts-watch": "tsc file1.ts file2.ts --watch"

然后删除单个文件声明,如果您有有效的 tsconfig.json,它应该可以工作:

"ts-watch": "tsc --watch"

这是一个有效的 tsconfig 示例:

{
  "compilerOptions": {
    "outDir": "lib",
    "sourceMap": true,
    "noImplicitAny": true,
    "lib": [
      "ES2015",
      "DOM"
    ],
    "module": "esnext",
    "target": "ES2015",
    "strict": true,
    "jsx": "react",
    "allowJs": true,
    "declaration": true,
    "moduleResolution": "node",
    "typeRoots": [
      "node_modules/@types/",
      "index.d.ts"
    ],
    "esModuleInterop": true
  },
  "include": [
    "./typescript/**/*",
  ]
}

【讨论】:

    【解决方案2】:

    要获得正确的类型定义,您有几个选择。

    如果您的 TypeScript 将在浏览器中运行

    将内置的“DOM”添加到您的编译器库(使用the --lib command line compiler optionlib in tsconfig.json)。如果您的 TypeScript 将在现代浏览器中运行,那么将“DOM”添加到库中可能是正确的选择。但是,如果您的 TypeScript 是在带有 fetch 模块的非浏览器环境中运行,请检查选项 2。

    如果您的 TypeScript 将在服务器 (node.js) 上运行

    如果您在节点上使用像 node-fetch 这样的库,则添加 @types/node-fetch 类型定义,并导入像 import fetch from "node-fetch" 这样的提取

    如果您的 TypeScript 同时在浏览器和服务器中运行(例如,对于服务器端渲染项目),请导入 DOM 库,您可能想要添加 isomorphic-fetch package

    【讨论】:

      【解决方案3】:

      我在 Angular(打字稿)项目中使用 fetch 时遇到了同样的错误,我可以通过像这样声明 fetch 来解决它:

        declare var fetch;
      

      【讨论】:

      • declare const fetch: Function
      猜你喜欢
      • 2018-01-25
      • 2016-09-15
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2017-06-18
      • 2018-11-28
      • 1970-01-01
      相关资源
      最近更新 更多