gpt4 book ai didi

typescript - 如何在我的 Typescript React Native 项目中获取 fetch 函数的类型定义?

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:42 25 4
gpt4 key购买 nike

我已经导入了 react 和 react-native 模块并为它们安装了类型定义(通过 typings )。

无法识别提取函数。我怎样才能让它工作?

src/reducers/calendar/actions.ts(16,5): error TS2304: Cannot find name 'fetch'.

最佳答案

2017-09-12 更新

此解决方案不起作用,因为当前的 Typescript 已弃用 typings.json

2016-10-28 更新

我忘了我之前修补过 react-native typings。我在 typings/globals/react-native/index.d.ts 中添加了这一行。

5643: export type fetch = (url: string, options?: Object) => Promise<any>
5644: export const fetch: fetch; // patch `fetch` not found problem

原创

我将以下行添加到文件的开头(在您的情况下为 action.ts)以解决此问题。

import {fetch} from "react-native";

我的 typings.json:

{
"globalDependencies": {
"fbemitter": "registry:dt/fbemitter#2.0.0+20160618175531",
"flux": "registry:dt/flux#0.0.0+20160618175531",
"immutable": "registry:dt/immutable#3.8.1+20160608070634",
"react": "registry:dt/react#0.14.0+20160927082313",
"react-dom": "registry:dt/react-dom#0.14.0+20160412154040",
"react-native": "registry:dt/react-native#0.29.0+20160928135220",
"react-native-button": "file:typings/custom/react-native-button/index.d.ts",
"react-native-fbsdk": "file:typings/custom/react-native-fbsdk/index.d.ts"
},
"dependencies": {
"react-native-router-flux": "npm:react-native-router-flux",
"rxjs": "npm:rxjs"
}
}

我的package.json:

{
"name": "SomeApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"dependencies": {
"flux": "^3.1.0",
"react": "15.3.2",
"react-native": "0.34.1",
"react-native-button": "^1.7.1",
"react-native-fbsdk": "^0.3.0",
"react-native-linkedin-login": "^1.3.0",
"react-native-localization": "^0.1.22",
"react-native-router-flux": "^3.35.0",
"react-native-scrollable-tab-view": "^0.6.0",
"rxjs": "^5.0.0-rc.1",
"typings": "1.4.0"
},
"devDependencies": {
"typescript": "2.0.3"
}
}

关于typescript - 如何在我的 Typescript React Native 项目中获取 fetch 函数的类型定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40156961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com