【发布时间】: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?
谢谢!
【问题讨论】:
-
@TomaszBubała 我做到了。就是这种情况 #1,但显然,如果没有
dom库(我猜它只打算用于仅浏览器的库),它就不会在没有错误的情况下编译:/ -
这不是你需要的直接答案,但你可以抽象出
fetch,就像在干净的架构中一样:8thlight.com/blog/uncle-bob/2012/08/13/…
标签: javascript node.js typescript fetch-api isomorphic-fetch-api