【问题标题】:ESLint linting my Typescript files correctlyESLint 正确检查我的 Typescript 文件
【发布时间】:2019-05-28 10:07:41
【问题描述】:

我有 .js 文件(src/client/...)和 .ts 文件(src/server/..)

我安装了以下软件包:

package.json

  "eslint": "^5.11.1",
  "eslint-config-airbnb-base": "^13.1.0",
  "eslint-config-typescript": "^1.1.0",
  "eslint-plugin-import": "^2.14.0",
  "eslint-plugin-typescript": "^0.14.0",
  ....
  "typescript": "^3.0.3",
  "typescript-eslint-parser": "^21.0.2",

我在 package.json 中定义了以下 aslant 配置

  "eslintConfig": {
    "extends": ["airbnb-base", "eslint:recommended", "typescript"],
    "env": { "es6": true, "browser": true },
    "parserOptions": { "parser": "typescript-eslint-parser" },
    "plugins": ["typescript"],
    "rules": {
      "no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }],
      "no-var": ["off"],
      "one-var": ["off"],
      "prefer-destructuring": ["error", { "object": false, "array": false } ],
      "no-underscore-dangle": ["error", { "allow": ["__blogPostData"] }]
    }
  }

然后我设置 lint 脚本:

 "lint": "eslint src --fix",

它正确列出了我的所有 .js 文件,但我的 .ts 文件中没有看到任何警告/错误(我怀疑它们都写得很好......)

我的打字稿 eslint 设置中是否有错误或遗漏任何内容?

【问题讨论】:

    标签: node.js typescript eslint


    【解决方案1】:

    parser 是根级配置项,不是parserOptions 的字段:

     "eslintConfig": {
        "parser": "typescript-eslint-parser"
        "parserOptions": { ...options },
        "plugins": ["typescript"]
      }
    

    此外,您使用的解析器和插件已被弃用。更新/维护的项目都可以在https://github.com/typescript-eslint/typescript-eslint 获得。 (配置相同,只是更新了名称)。这是关于如何设置它的一个很好的分步演练 - https://dev.to/robertcoopercode/using-eslint-and-prettier-in-a-typescript-project-53jb

    请注意,您可以使用单独的.eslintrc.{js,yaml},而不是在 package.json 中定义您的 eslint 配置。根目录中的一个将为 JavaScript 设置标准规则,./src/server 中的另一个可以仅为相关目录设置 TypeScript 解析器和插件(但仍会选择所有标准根规则)。

    【讨论】:

    【解决方案2】:

    你应该为 TypeScript 使用 tslint,而不是 eslint:

    npm i tslint --save
    

    之后,在您的项目根目录中创建一个 tslint.json(或从 Web 中选择一个您喜欢的规则)。只是我在 Google 上找到的一个链接:ts lint setup

    【讨论】:

    • 非常感谢@Bronco...要设置它...我还将编写 2 个脚本:lint-client(eslint .js 客户端文件和 lint-server(ts lint.ts节点服务器文件)
    • 已解决..运行正常
    • 如果您回答“我如何使用技术 A?”,请提供理由。是“改用技术 B”。但是,不应使用 tslint,因为 Microsoft 已弃用它,转而支持 eslint - github.com/Microsoft/TypeScript/issues/…(当然,此开发发生在 答案发布后 6 天)
    猜你喜欢
    • 2021-03-04
    • 2019-11-14
    • 2021-01-03
    • 1970-01-01
    • 2019-07-30
    • 2020-04-05
    • 2020-02-08
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多