TypeScript 学习 -代码检查工具 eslint
代码检查工具
尽管 TypeScript 提供了类型检查和静态分析功能,使用 ESLint 仍然能带来很多好处,特别是当需要确保代码质量、遵循一致的代码风格、避免潜在的错误和提高团队协作时。ESLint 和 TypeScript 是互补的工具,它们共同帮助你保持高质量的代码。
vscode 插件 ESLint
版本是: 3.0.10
package.json
增减依赖:
"devDependencies": {
"@eslint/js": "^9.18.0",
"eslint": "^9.18.0",
"globals": "^15.14.0",
"typescript": "^5.7.3",
"typescript-eslint": "^8.21.0"
}
eslint.config.mjs
:
可以使用 npx eslint --init
生成配置文件
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
/** @type {import('eslint').Linter.Config[]} */
export default [
{files: ["src/**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
如果发现 eslint
没有生效, 可以看看 vscode 的插件和全局安装的 eslint
.
可以尝试升级全局的 eslint
:
yarn global upgrade eslint