[Typescript] Step 4. ESLint for Typescript

Step 4: ESLint

We need to install ESLint tools for Typescript.

yarn add -D @typescript-eslint/eslint-plugin @typescript-eslint/parser

 

.eslintrc file:

"extends": [
    "eslint:recommended",
+    "prettier",
    "plugin:promise/recommended",
    "plugin:sonarjs/recommended",
+    "plugin:@typescript-eslint/recommended",
+    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],

We want eslint to backoff when prettier do its job.

https://github.com/prettier/eslint-plugin-prettier

https://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md#version-800-2021-02-21

.eslintrc:

查看代码
 {
  "env": {
    "es2021": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "extends": [
    "eslint:recommended",
    "prettier",
    "plugin:promise/recommended",
    "plugin:sonarjs/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 12,
    "project": "tsconfig.eslint.json"
  },
  "plugins": [
    "react",
    "@typescript-eslint",
    "promise",
    "sonarjs",
    "prettier"
  ],
  "rules": {
    "prefer-const": "error"
  },
  "overrides": [
    /**
     * CLIENT SIDE CODE
     */
    {
      "files": ["src/**/*.{ts,js,jsx,tsx}"],

      "env": {
        "browser": true,
        "es2021": true
      },
      "rules": {
        "react/prop-types": "off",
        "react/no-children-prop": "off"
      },
      "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "prettier"
      ]
    },
    /**
     * SERVER SIDE CODE
     */
    {
      "extends": ["plugin:node/recommended"],
      "files": [
        "config/**/*.js",
        "babel.config.js",
        "tailwind.config.js",
        "postcss.config.js",
        "server/**/*.js"
      ],
      "env": { "commonjs": true, "node": true }
    },
    /**
     * TYPESCRIPT CODE
     */
    {
      "files": ["{src,tests}/**/*.{ts,tsx}"],
      "extends": [
        "prettier",
        "plugin:@typescript-eslint/recommended",
        "plugin:@typescript-eslint/recommended-requiring-type-checking"
      ],
      "rules": {
        "no-unused-vars": "off",
        "@typescript-eslint/no-unsafe-call": "off",
        "@typescript-eslint/restrict-template-expressions": "off",
        "@typescript-eslint/no-unsafe-member-access": "off",
        "@typescript-eslint/no-unsafe-assignment": "off",
        "@typescript-eslint/no-unsafe-return": "off",
        "@typescript-eslint/no-explicit-any": "off"
      }
    },
    /**
     * TESTS
     */
    {
      "files": ["tests/**/*.{js,jsx,ts,tsx}"],
      "extends": [],
      "env": { "node": true, "jest": true }
    }
  ]
}

 

You can run yarn lintto see result.

Sometimes, ESlint might report you some files which you don't care about, for example server side, javascript code, then what you can do is disableing those rules in config file:

.eslintrc:

/**
     * SERVER SIDE CODE
     */
    {
      "extends": ["plugin:node/recommended"],
      "files": [
        "config/**/*.js",
        "babel.config.js",
        "tailwind.config.js",
        "postcss.config.js",
        "server/**/*.js"
      ],
+      "rules": {
+        "@typescript-eslint/no-unsafe-member-access": "off",
+        "@typescript-eslint/no-var-requires": "off",
+        "@typescript-eslint/no-unsafe-assignment": "off",
+        "@typescript-eslint/no-unsafe-argument": "off",
+        "@typescript-eslint/no-unsafe-call": "off",
+        "@typescript-eslint/unbound-method": "off",
+      },
      "env": { "commonjs": true, "node": true }
    },

 

See this example commit: https://github.com/zhentian-wan/professional-ts/commit/154e5c83c0dcf6bcdfec79f2584c5cfe060342d3

 

Now, we can continue tell Eslint "no-explicit-any":

"rules": {
        "no-unused-vars": "off",
        "@typescript-eslint/no-unsafe-call": "off",
        "@typescript-eslint/restrict-template-expressions": "off",
        "@typescript-eslint/no-unsafe-member-access": "off",
        "@typescript-eslint/no-unsafe-assignment": "off",
        "@typescript-eslint/no-unsafe-return": "off",
-        "@typescript-eslint/no-explicit-any": "off"
+        "@typescript-eslint/no-explicit-any": "error"
      }

 

See this example commit: https://github.com/zhentian-wan/professional-ts/commit/63c90248833fa811a566bbbd7514158642274f28

posted @   Zhentiw  阅读(348)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-08-29 [React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks
2019-08-29 [CSS] Conditionally Assign Style to a Parent Element with Focus-Within Pseudo-class
2019-08-29 [Angular 8] Calculate and Measure Performance budgets with the Angular CLI
2019-08-29 [Angular 8] Custom Route Preloading with ngx-quicklink and Angular
2019-08-29 [Angular 8] Implement a Custom Preloading Strategy with Angular
2019-08-29 [Angular 8] Lazy loading with dynamic loading syntax
2018-08-29 [React] Prevent Unnecessary Rerenders of Compound Components using React Context
点击右上角即可分享
微信分享提示