首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >eslint-plugin-flowtype类型不验证

eslint-plugin-flowtype类型不验证
EN

Stack Overflow用户
提问于 2016-11-23 15:24:33
回答 4查看 3.8K关注 0票数 5

我正在尝试配置eslint + babel-eslint + eslint-plugin-react + eslint-plugin-flowtype

我有下面的devDependencies ( package.json )

代码语言:javascript
复制
"babel-eslint": "^7.1.1",
"eslint": "^3.10.2",
"eslint-config-airbnb": "^13.0.0",
"eslint-plugin-flowtype": "^2.25.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.7.1"

和下面的.eslinrc

代码语言:javascript
复制
{
  "parser": "babel-eslint",
  "plugins": [
    "flowtype"
  ],
  "extends": [
    "airbnb",
    "plugin:flowtype/recommended"
  ],
  "rules": {
    "max-len": [1, 80, 2, {"ignoreComments": true}],
    "prop-types": [0],
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
    "react/prefer-stateless-function": [
      0,
      {
        "ignorePureComponents": true
      }
    ],
    "no-console": 0
  },
  "settings": {
    "flowtype": {
      "onlyFilesWithFlowAnnotation": true
    }
  }
}

我用App.js编写了简单的代码示例

代码语言:javascript
复制
function foo() {
  const a: string = 1;
  return a;
}

async function bar() {
  const b = await foo();
  return b;
}

如果我启动eslint src/App.js,那么eslint就不会显示任何消息。如果我将"flowtype/require-return-type": 2添加到.eslintrc中,那么eslint显示:

代码语言:javascript
复制
error  Missing return type annotation  flowtype/require-return-type
error  Missing return type annotation  flowtype/require-return-type
✖ 2 problems (2 errors, 0 warnings)

但我不明白为什么const a: string = 1;是有效的。如何启用const a: string = 1;的检查类型

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-11-23 15:56:11

eslint-plugin-flowtype而不是流。它是对ESLint的一个扩展,它拥有一组仅与Flow的附加语法相关的lint规则。

例如,有一个规则允许在流对象类型中强制使用逗号或分号(例如,type Foo = {bar: string, baz: number}type Foo = {bar: string; baz: number})。

但是,要真正实现打字机功能,您需要安装,并按照说明进行设置。简而言之,它包括确保在项目根上有一个.flowconfig文件,确保在所有文件上都有// @flow头,然后从命令行运行flow status (或者使用核素或其他具有流支持的编辑器)。

票数 6
EN

Stack Overflow用户

发布于 2018-01-02 14:34:53

如果您想让ESLint use来验证您的代码,正确的插件是eslint-plugin-flowtype-errors

https://www.npmjs.com/package/eslint-plugin-flowtype-errors

正如其他人所写的,eslint-plugin-flowtype只验证带有FlowType语句的代码在语法上是正确的。它实际上不进行流类型验证。

票数 3
EN

Stack Overflow用户

发布于 2016-11-23 15:54:22

eslint-plugin-flowtype 似乎没有一个规则来检查.

linter的目的是执行文体或常规事物(例如总是注释函数返回类型),而不是检查主题类型。

您需要运行Flow本身来检查这些类型是否确实正确。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40768266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档