mcpField
T
typescript-eslint

typescript-eslint

github.com/claude-dev-suite测试与质量4685 次安装2 天前更新
代码仓库

关于 typescript-eslint

为 TypeScript 项目配置 typescript-eslint 的参考技能,涵盖 ESLint 设置、类型感知规则和从 TSLint 迁移,并提供配置示例与故障排查指导。

主要功能

  1. 01TypeScript 的 ESLint 配置
  2. 02类型感知检查预设
  3. 03常见规则示例
  4. 04按文件配置规则
  5. 05故障排查指导

适用场景

  1. 01在 TypeScript 项目中配置 ESLint
  2. 02启用类型感知检查
  3. 03从 TSLint 迁移

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/claude-dev-suite/claude-dev-suite --skill typescript-eslint

安装后在 Claude Code 中输入/typescript-eslint确认已加载。

详细介绍

typescript-eslint - 快速参考

何时使用此技能

  • 为 TypeScript 项目配置 ESLint
  • 使用面向类型的 TypeScript 规则
  • 从 TSLint 迁移

何时不使用此技能

  • 完整的 ESLint 9 配置 - 使用 eslint-biome 技能进行完整配置
  • Biome 代码检查器 - 使用 eslint-biome 技能配置 Biome
  • 代码质量原则 - 使用 quality-common 学习 SOLID/整洁代码
  • Java/Kotlin - 使用 sonarqube 技能处理 JVM 语言

深入了解:使用 mcp__documentation__fetch_docs,并将 technology 设为 typescript-eslint,以获取完整文档。

基本设置

npm install -D eslint typescript-eslint

Flat Config(ESLint 9+)

eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.recommended,
);
面向类型的代码检查
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.recommendedTypeChecked,
  {
    languageOptions: {
      parserOptions: {
        projectService: true,
        tsconfigRootDir: import.meta.dirname,
      },
    },
  },
);
严格配置
export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.strictTypeChecked,
  ...tseslint.configs.stylisticTypeChecked,
);

常用规则

{
  rules: {
    // Prevent unused variables
    '@typescript-eslint/no-unused-vars': ['error', {
      argsIgnorePattern: '^_'
    }],

    // Require explicit return types
    '@typescript-eslint/explicit-function-return-type': 'warn',

    // Prevent any usage
    '@typescript-eslint/no-explicit-any': 'error',

    // Require await in async functions
    '@typescript-eslint/require-await': 'error',

    // Prevent floating promises
    '@typescript-eslint/no-floating-promises': 'error',

    // Consistent type imports
    '@typescript-eslint/consistent-type-imports': 'error',
  }
}

针对特定文件的规则

export default tseslint.config(
  // ... base config
  {
    files: ['**/*.test.ts', '**/*.spec.ts'],
    rules: {
      '@typescript-eslint/no-explicit-any': 'off',
    },
  },
  {
    files: ['**/*.js'],
    ...tseslint.configs.disableTypeChecked,
  },
);

反模式

反模式不良影响正确做法
不使用面向类型的规则无法发现 Promise、异步相关问题使用 recommendedTypeChecked
到处允许 any违背 TypeScript 的初衷设置 no-explicit-any: error
没有 require-await 规则使用不必要的 async 关键字启用面向类型的异步规则
对 JS 文件进行类型检查JS 没有类型对 .js 使用 disableTypeChecked
在测试文件中禁用规则测试同样需要保证质量仅在需要时禁用 no-explicit-any
忽略未使用的变量代码异味、未使用的导入使用带忽略模式的 no-unused-vars

快速故障排除

问题可能原因解决方案
面向类型的规则不起作用缺少 projectService: true添加到 languageOptions.parserOptions
代码检查非常慢对所有文件进行类型检查仅将类型规则限制在 **/*.ts
到处出现“Unsafe”错误严格类型检查使用 strictTypeChecked 或禁用特定规则
未捕获 Promise 错误缺少 no-floating-promises在 recommendedTypeChecked 预设中启用
未检测到未使用的导入规则配置错误使用 @typescript-eslint/no-unused-vars
规则与 Prettier 冲突两者都在格式化代码最后添加 eslint-config-prettier

评论

支持 Markdown