【问题标题】:Fetch not covered by FlowFlow 未覆盖 Fetch
【发布时间】:2017-12-22 06:20:14
【问题描述】:

我正在为 fetch API 和 react-native 应用程序中 flowjs 的实现而苦苦挣扎。

使用 Nuclide,我可以看到 Flow 没有涵盖我用来从远程 API 获取数据的 fetch 函数...

是的,如果我 99% 的代码受到监控但仍然受到监控,这没什么大不了的。我想了解发生了什么。

我已经安装了流式。我已经为我的 fetch 创建了一个存根,我可以在 flow-typed/npm/fbjs_vx.x.x.x 中看到它,这里是:

declare module 'fbjs/lib/fetch' {
  declare module.exports: any;
}

我也尝试下载替代的 ponyfill 或 polyfill,以便在没有任何成功的情况下覆盖该行。

如果没有 libdef 已经存在,我怎么能说“获取一个字符串作为参数并返回一个承诺”来流动?

感谢您的帮助

更新

这是 package.json

{
  "name": "BeerOmatic",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "flow": "flow",
    "android": "ENVFILE=.env.dev react-native run-android"
  },
  "dependencies": {
    "eslint-config-airbnb-flow": "^1.0.2",
    "eslint-plugin-flowtype": "^2.35.0",
    "fetch-ponyfill": "^4.1.0",
    "flow-typed": "^2.1.2",
    "react": "16.0.0-alpha.12",
    "react-native": "0.46.3",
    "react-native-config": "^0.5.0",
    "react-native-fetch-polyfill": "^1.1.2",
    "react-native-navigation": "^1.1.136",
    "react-redux": "^5.0.5",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0",
    "whatwg-fetch": "^2.0.3"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-eslint": "^7.2.3",
    "babel-jest": "20.0.3",
    "babel-preset-flow": "^6.23.0",
    "babel-preset-react-native": "2.1.0",
    "eslint": "^3.19.0",
    "eslint-config-airbnb": "^15.0.2",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-react": "^7.1.0",
    "eslint-plugin-react-native": "^2.3.2",
    "flow-bin": "^0.50.0",
    "jest": "20.0.4",
    "react-test-renderer": "16.0.0-alpha.12"
  },
  "jest": {
    "preset": "react-native"
  }
}

【问题讨论】:

  • 可以添加 package.json 文件吗?

标签: javascript react-native flowtype fetch-api flow-typed


【解决方案1】:

嗯...大多数时候,答案都在文档中...

要声明一个可以在整个项目中访问的全局函数,请在 libdef 文件中使用 declare function 语法:

流式/myLibDef.js

声明函数 foo(a: number): string; 这告诉 Flow 项目中的任何代码都可以引用 foo 全局函数,并且该函数接受一个参数(一个数字)并返回一个字符串。

所以我添加了那个

declare function fetch (a: string): Promise;

到我的流式文件夹根目录下的一个全新的 myLibDef.js 文件。 然后令人惊讶的是,我不得不将它添加到我的 .flowConfig 中(在 libs 部分)。我很确定它是自动的,但是……嗯…… 无论如何,现在 fetch 被覆盖了! \0/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2011-03-20
    相关资源
    最近更新 更多