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

时间:2016-10-20 14:14:38

标签: typescript react-native

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

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

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

1 个答案:

答案 0 :(得分:1)

2017-09-12更新

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

2016-10-28更新

我忘记了之前我修补了原子的反应。我在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"
  }
}