使用 eslint-plugin-flowtype,如何检查类型是否存在

With eslint-plugin-flowtype, how to check if a type exist

我在 React 项目上启用了 eslint-plugin-flowtype 和 Flow。它工作得很好但并不完美。例如,我有因缺少 returned 类型而引发的错误 Missing return type annotation. (flowtype/require-return-type)。但是当我写一个未知类型时,我没有错误。例如,写入 {dispatch: TotoType} 不会引发任何错误(TotoType 未声明)。

更清楚:

我的问题是,如果我写了一个不存在的类型(我的意思是 nomber 而不是 number),Flowtype eslint 插件不会 return 任何错误。每当我使用它时都应该触发错误,但事实并非如此:(。如果我在我的文件上使用 Flow CLI,我会收到类似 unknown Type 的错误。我想让 FlowType ESlint 插件检查相同的错误.

我正在使用提供这些规则的 eslint-config-evenium 配置文件:

{
    ...
    "rules": {

        "flowtype/boolean-style": [
            2,
            "boolean"
        ],
        "flowtype/define-flow-type": 1,
        "flowtype/delimiter-dangle": [
            2,
            "never"
        ],
        "flowtype/generic-spacing": [
            2,
            "never"
        ],
        "flowtype/no-primitive-constructor-types": 2,
        "flowtype/no-weak-types": 0,
        "flowtype/object-type-delimiter": [
            2,
            "comma"
        ],
        "flowtype/require-parameter-type": 2,
        "flowtype/require-return-type": [
            2,
            "always",
            {
                "annotateUndefined": "never",
                "excludeArrowFunctions": true  
            }
        ],
        "flowtype/require-valid-file-annotation": 2,
        "flowtype/semi": [
            2,
            "always"
        ],
        "flowtype/space-after-type-colon": [
            2,
            "always"
        ],
        "flowtype/space-before-generic-bracket": [
            2,
            "never"
        ],
        "flowtype/space-before-type-colon": [
            2,
            "never"
        ],
        "flowtype/type-id-match": [
            2,
            "^([A-Z][a-z0-9]+)+Type$"
        ],
        "flowtype/union-intersection-spacing": [
            2,
            "always"
        ],
        "flowtype/use-flow-type": 1,
        "flowtype/valid-syntax": 1
    },
    "settings": {
        "flowtype": {
            "onlyFilesWithFlowAnnotation": true
        }
    }
}

如何让 ESlint 为未经授权的类型生成错误? 如果可能的话,我将很高兴能够将不存在的类型添加到授权类型列表中。

有谁知道该怎么做,或者这是否可能?

eslint-plugin-flowtype 仅验证用于编写流类型代码的语法。它根本没有 运行 Flowtype。

要在您的代码中使用 Flowtype 运行ning,您有几种解决方案:

  • 使用flow-bin在命令行中启动 Flow
  • 使用 flow-bin + gulp 创建将启动 Flow 服务器的任务:

例如:

gulp.task('flow', () => {
  execFile(flow, ['status'], (err, stdout) => {
    console.log(stdout);
  });
});
  • 使用gulp-flowtype。它像以前的解决方案(flow-bin + gulp)除了 gulp 进程可以被阻塞等待 Flowtype 服务器启动,你可以在结束时停止 Flowtype 服务器gulp 进程。
  • 使用 IDE 中的 Flowtype。您的 IDE 已经可以提供它(又名 Nuclide) or you can find an existing plugin. For example, I use flow-for-vscode 和 VSCode。