gpt4 book ai didi

javascript - eslint-plugin-flowtype 不验证

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:48 25 4
gpt4 key购买 nike

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

我在 package.json 中有以下 devDependencies:

"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:

{
"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 中编写了简单的代码示例:

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 显示:

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; 的检查类型?

最佳答案

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

例如,有一个rule允许您强制在 Flow 对象类型中使用逗号还是分号(例如 type Foo = {bar: string, baz: number} vs type Foo = {bar: string; baz: number })

但是,要真正进行类型检查,您需要安装 Flow ,并按照那里的说明进行设置。简而言之,它涉及确保在项目根目录下有一个 .flowconfig 文件,确保所有文件上都有 //@flow header ,以及然后从命令行运行 flow status(或使用 Nuclide 或其他支持 Flow 的编辑器)。

关于javascript - eslint-plugin-flowtype 不验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com