在 TSLint Roadmap 规划中,未来 ESLint 会逐渐替换 TSLint ,本篇主要讲解 TypeScript 使用 ESLint 相关配置和使用。

官方文档

基础使用

  • 安装依赖

    1. $ yarn add -D eslint eslint-plugin-react @typescript-eslint/parser @typescript-eslint/eslint-plugin
  • .eslintrc.js

    1. module.exports = {
    2. env: {
    3. browser: true,
    4. es6: true
    5. },
    6. extends: [
    7. "eslint:recommended",
    8. "plugin:react/recommended",
    9. "plugin:@typescript-eslint/eslint-recommended"
    10. ],
    11. parser: "@typescript-eslint/parser",
    12. parserOptions: {
    13. ecmaFeatures: {
    14. jsx: true
    15. },
    16. ecmaVersion: 2018,
    17. sourceType: "module"
    18. },
    19. plugins: ["react", "@typescript-eslint"],
    20. rules: {
    21. "no-unused-vars": "off",
    22. "@typescript-eslint/indent": ["error", 2],
    23. "@typescript-eslint/no-unused-vars": "warn"
    24. }
    25. }

规范检测

  1. // package.json
  2. {
  3. "scripts:{
  4. "eslint": "eslint . --ext .ts,tsx",
  5. "fix": "eslint . --ext .ts,tsx --fix",
  6. }
  7. }