react-native - Flow 为新项目生成许多错误

标签 react-native flowtype

Flow 似乎在新项目上产生了很多错误。我做错了什么?

我做了什么:

  • 使用 react-native init FlowProject 创建了一个新的 React Native 项目
  • 已关注 set-up for Flow with yarn and Babel
    • yarn add --dev babel-cli babel-preset-flow
    • yarn add --dev flow-bin
  • 运行 yarn run flow(.flowconfig 由 init 生成)

这是我得到的错误:

.flowconfig:53 Unsupported option specified! (unsafe.enable_getters_and_setters)
error Command failed with exit code 8.

现在如果我删除指定的行(和版本号),我会得到一堆这两个错误:

Error: node_modules/react-native/Libraries/Inspector/Inspector.js:104
104:       clearTimeout(_hideWait);
                        ^^^^^^^^^ null. This type is incompatible with the expected param type of
733: declare function clearTimeout(timeoutId?: TimeoutID): void;
                                               ^^^^^^^^^ TimeoutID. See lib: /private/tmp/flow/flowlib_171966cc/core.js:733

Error: node_modules/react-native/Libraries/Lists/VirtualizedList.js:1094
1094:         this.props.onEndReachedThreshold * visibleLength / 2;
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ undefined. The operand of an arithmetic operation must be a number.

Found 19 errors
error Command failed with exit code 2.

如果我删除生成的 .flowconfig 文件(见最后),使用 yarn run flow init 重新创建它,然后再次运行 yarn run flow,我得到以下错误(我只展示了这 5 个,其中大部分彼此相似):

Error: node_modules/react-native/Libraries/Animated/src/AnimatedEvent.js:101
101:     if (__DEV__) {
             ^^^^^^^ __DEV__. Could not resolve name

Error: node_modules/react-native/Libraries/Inspector/Inspector.js:104
104:       clearTimeout(_hideWait);
                        ^^^^^^^^^ null. This type is incompatible with the expected param type of
733: declare function clearTimeout(timeoutId?: TimeoutID): void;
                                               ^^^^^^^^^ TimeoutID. See lib: /private/tmp/flow/flowlib_15451c3f/core.js:733

Error: node_modules/react-native/Libraries/Interaction/InteractionManager.js:107
107:           return promise.done(...args);
                      ^^^^^^^^^^^^^^^^^^^^^ call of method `done`. Function cannot be called on
107:           return promise.done(...args);
                      ^^^^^^^^^^^^^^^^^^^^^ property `done` of unknown type

Error: node_modules/react-native/Libraries/Interaction/TaskQueue.js:165
165:       .done();
            ^^^^ property `done`. Property not found in
         v---------
152:     task.gen()
153:       .then(() => {
154:         DEBUG && infoLog(
...:
164:       })
           -^ Promise

Error: node_modules/react-native/Libraries/Lists/VirtualizedList.js:1016
1016:       distanceFromEnd < onEndReachedThreshold * visibleLength &&
                              ^^^^^^^^^^^^^^^^^^^^^ undefined. The operand of an arithmetic operation must be a number.


... 16 more errors (only 50 out of 66 errors displayed)
To see all errors, re-run Flow with --show-all-errors
error Command failed with exit code 2.

现在我知道我可以告诉 flow 忽略某些文件,但首先,我觉得 flow 给我这些我没有生成的代码的错误似乎很奇怪。我还在 github 问题上看到我们不应该告诉流程忽略 node_modules。那么忽略第一个流分析给我们的每个文件的解决方案是什么?

这是相关文件的列表:

Error node_modules/react-native/Libraries/Inspector/Inspector.js:
Error node_modules/react-native/Libraries/Interaction/InteractionManager.js
Error node_modules/react-native/Libraries/Lists/FlatList.js
Error node_modules/react-native/Libraries/Lists/ViewabilityHelper.js
Error node_modules/react-native/Libraries/Lists/VirtualizedList.js
Error node_modules/react-native/Libraries/Lists/VirtualizedSectionList.js
Error node_modules/react-native/Libraries/StyleSheet/flattenStyle.js

生成的 .flowconfig 文件

[ignore]
; We fork some components by platform
.*/*[.]android.js

; Ignore "BUCK" generated dirs
<PROJECT_ROOT>/\.buckd/

; Ignore unexpected extra "@providesModule"
.*/node_modules/.*/node_modules/fbjs/.*

; Ignore duplicate module providers
; For RN Apps installed via npm, "Libraries" folder is inside
; "node_modules/react-native" but in the source repo it is in the root
.*/Libraries/react-native/React.js

; Ignore polyfills
.*/Libraries/polyfills/.*

; Ignore metro
.*/node_modules/metro/.*

[include]

[libs]
node_modules/react-native/Libraries/react-native/react-native-interface.js
node_modules/react-native/flow/
node_modules/react-native/flow-github/

[options]
emoji=true

module.system=haste

munge_underscores=true

module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'

module.file_ext=.js
module.file_ext=.jsx
module.file_ext=.json
module.file_ext=.native.js

suppress_type=$FlowIssue
suppress_type=$FlowFixMe
suppress_type=$FlowFixMeProps
suppress_type=$FlowFixMeState

suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError

unsafe.enable_getters_and_setters=true

[version]
^0.61.0

最佳答案

你不能删除 React Native .flowconfig,因为它定义了很多项目需要的自定义配置,以便干净地进行类型检查。

让我们看看初始错误:

.flowconfig:53 Unsupported option specified! (unsafe.enable_getters_and_setters) error Command failed with exit code 8.

这是因为您使用 yarn add flow-bin 安装的 Flow 版本不再支持此选项(原为 removed in 0.62)。您需要安装在 .flowconfig 的最后一行定义的准确版本的 Flow

[version]
^0.61.0

你可以这样做:

yarn add flow-bin@0.61

现在项目应该正确输入检查。

请注意,将来如果您使用react-native upgrade 升级您的项目模板并接受对.flowconfig 的更改,您可能需要手动升级您的要匹配的 flow-bin 依赖项。

关于react-native - Flow 为新项目生成许多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48186173/

相关文章:

react-native - React native 导航选项卡注销功能

ios - 如何在 iOS 的调试构建配置中禁用 React Native 开发模式?

reactjs - 使用 React.Node 时流类型错误

javascript - 跨对象保留此上下文

react-native - 使用react-native-router-flux自定义navBar

javascript - 具有标准的 Flowtype 无法加载插件 flowtype--parser

flowtype - 对象不能有键

flowtype - 忽略 node_modules 但不是 flowtype 中的一些

javascript - 流可选类型参数

azure - 使用react-native管理azure blob