javascript – Typescript – 找不到名称’fetch'(通用库)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Typescript – 找不到名称’fetch'(通用库),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3531字,纯文字阅读大概需要6分钟。
内容图文
我的目标是使用AJAX调用(使用fetch API)构建一个Typescript库,客户端(Webpack / Browserify)和后端开发人员(Node)都可以使用它.
但是,我似乎无法在没有错误的情况下进行编译.
我的第一次尝试是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 lib它可以编译并在两个上工作?但是我无法控制它是否真的可以在Node上运行.我的意思是,它会编译我是否导入’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?
谢谢!
解决方法:
我在一个angular(typescript)项目中使用fetch时遇到了同样的错误,我可以通过声明这样的fetch来解决它:
declare var fetch;
内容总结
以上是互联网集市为您收集整理的javascript – Typescript – 找不到名称’fetch'(通用库)全部内容,希望文章能够帮你解决javascript – Typescript – 找不到名称’fetch'(通用库)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。