【问题标题】:How do I get the type definition for the fetch function in my Typescript React Native project?如何在我的 Typescript React Native 项目中获取 fetch 函数的类型定义?
【发布时间】:2016-10-20 14:14:37
【问题描述】:

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

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

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

【问题讨论】:

  • 似乎较新版本的 TypeScript 内置了 fetch 定义,但我发现 Axios 等库内置了包括泛型在内的类型定义,因此使用起来更好

标签: typescript react-native


【解决方案1】:

2017 年 9 月 12 日更新

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

2016 年 10 月 28 日更新

我忘了我之前修补过 react-native 类型。我在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"
  }
}

【讨论】:

  • 对我不起作用。我得到src/reducers/calendar/actions.ts(18,5): error TS2304: Cannot find name 'fetch'. 我正在使用这个类型定义文件(来自typings.json):`“react-native”:“registry:dt/react-native#0.29.0+20160830141535”`
  • 我正在使用这种类型定义:“react-native”:“registry:dt/react-native#0.29.0+20160928135220”。好像我的比较新。让我看看有什么变化
  • @cubabit 嗨,我是 react-native.d.ts 的维护者。 fetch 被导出为使用 export type fetch = (url: string, options?: Object) =&gt; Promise&lt;any&gt; 的类型。这个定义当然可以改进——如上面的 TODO 所示——但确实存在。你能检查你的 d.ts 文件吗?
  • @cubabit 对不起,我忘记了我之前在本地修补了打字。请参考这个[问题]。(github.com/DefinitelyTyped/DefinitelyTyped/issues/12198)。我正在提交拉取请求。
猜你喜欢
  • 2020-10-03
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2015-07-15
  • 1970-01-01
  • 2020-08-05
  • 2019-03-13
相关资源
最近更新 更多