如何在不触发eslint异常的情况下导入流类型?

时间:2018-02-20 20:57:10

标签: reactjs import eslint flowtype

我无法在不触发夹板错误的情况下导入项目中的流类型

project/flow-typed/auth.js

declare module 'auth' {
  declare type TOKEN = {
    email: string,
    password: string,
    type: string,
    deviceType: string,
  };
}

file.js

// @flow

import type { TOKEN } from 'auth';

$ eslint src

3:1   error  'auth' should be listed in the project's dependencies. Run 'npm i -S auth' to add it  import/no-extraneous-dependencies
3:24  error  Unable to resolve path to module 'auth'                                               import/no-unresolved
3:24  error  Missing file extension for "auth"                                                     import/extensions

.eslintrc

{
  "extends": [ "plugin:flowtype/recommended", "airbnb", "prettier", "prettier/react" ],
  "plugins": [ "flowtype", "prettier" ],
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 2016,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true,
    "jest": true
  },
  "rules": {
    "react/jsx-filename-extension": false,
    "react/prop-types": false,
    "no-console": "off"
  }
}

package.json devDependencies

  "devDependencies": {
    "babel-eslint": "^8.2.1",
    "eslint": "^4.17.0",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-config-react": "^1.1.7",
    "eslint-plugin-flowtype": "^2.45.0",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-prettier": "^2.6.0",
    "eslint-plugin-react": "^7.6.1",
    "flow-bin": "^0.66.0",
    "prettier": "^1.10.2"
  }

流程执行没有错误。

正如您所见,lint 能够识别出我正在从模块导入类型。我正在使用flowtype插件并扩展flowtype / recommended规则。

出了什么问题,我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

我不认为eslint可以告诉"模块"存在于类型级别。我不认为这个eslint插件旨在识别您从模块导入类型。更常见的情况是有人从该模块导入模块和(可能)类型。您有几个选择:

  • 安装auth模块,不再抱怨。我不确定为什么你在没有使用模块的情况下使用它的类型,所以也许这不是你想要/需要做的事情。
  • TOKEN类型移到其他位置。您未使用auth模块,因此您似乎不需要将该类型添加到auth模块中。
  • (可能)为eslint写一个resolver来找到你的typedef并停止抱怨。由于额外的工作,我不建议使用此选项,上述两个选项之一应涵盖您尝试做的任何事情。