You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求推荐适用于Node+TypeScript的开箱即用ESLint插件

推荐方案:使用整合式ESLint配置预设

不用手动逐个适配插件的TypeScript兼容性,以下是覆盖你需求的开箱即用方案:

1. 手动组合核心插件(可控性高)

先安装所有依赖:

npm install --save-dev eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-import eslint-plugin-security eslint-plugin-sonarjs eslint-plugin-n

在项目根目录创建.eslintrc.js,配置已自动处理TypeScript适配:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    project: './tsconfig.json', // 必须指定tsconfig以启用TypeScript类型校验规则
  },
  plugins: ['@typescript-eslint', 'import', 'security', 'sonarjs', 'n'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'plugin:import/recommended',
    'plugin:import/typescript',
    'plugin:security/recommended',
    'plugin:sonarjs/recommended',
    'plugin:n/recommended'
  ],
  rules: {
    // 可根据项目需求微调规则,例如:
    '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
    'n/no-unpublished-require': 'off'
  },
};

2. 全量预设(零配置成本)

使用eslint-config-alloy,它已整合TypeScript、import、基础安全规则,可直接扩展sonarjs:

npm install --save-dev eslint typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-alloy eslint-plugin-sonarjs eslint-plugin-n

配置文件.eslintrc.js:

module.exports = {
  extends: [
    'alloy',
    'alloy/typescript',
    'plugin:sonarjs/recommended',
    'plugin:n/recommended'
  ],
  parserOptions: {
    project: './tsconfig.json',
  },
};

3. Node.js项目专属预设

如果是Node.js项目,用eslint-config-standard-with-typescript,它已整合eslint-plugin-n(即你提到的n插件):

npm install --save-dev eslint eslint-config-standard-with-typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-security eslint-plugin-sonarjs

配置文件:

module.exports = {
  extends: [
    'standard-with-typescript',
    'plugin:security/recommended',
    'plugin:sonarjs/recommended'
  ],
  parserOptions: {
    project: './tsconfig.json',
  },
};

关键注意点

  • 确保项目根目录存在tsconfig.json,且parserOptions.project路径正确,否则TypeScript专属规则无法生效。
  • 若安装时出现版本冲突,可添加--legacy-peer-deps参数(npm 7+适用)解决。

内容的提问来源于stack exchange,提问作者Alex Misiulia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 17:02:11