Flow为新项目生成许多错误

时间:2018-01-10 11:13:39

标签: 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
  • Ran 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,我会收到以下错误(我只是显示了这些错误,他们中的大多数彼此相似):

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.

现在我知道我可以告诉流程忽略某些文件,但首先,对我来说这似乎很奇怪,流程给了我这些我没有生成的代码的错误。我也在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

3 个答案:

答案 0 :(得分:4)

您无法删除React Native .flowconfig,因为它定义了项目要求干净地进行类型检查所需的大量自定义配置。

让我们看一下初始错误:

  

.flowconfig:53指定了不支持的选项! (unsafe.enable_getters_and_setters)   错误命令失败,退出代码为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依赖项以匹配。< / p>

答案 1 :(得分:0)

找到这个半相关的issue,建议更新flowconfig以使用latest version from create-react-native-app。似乎对我有用。

答案 2 :(得分:0)

转到根项目文件夹中的 .flowconfig 文件。

删除这一行:

esproposal.optional_chaining=enable
esproposal.nullish_coalescing=enable

Lines to delete

完成!