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

解决ESLint中@stylistic/type-annotation-spacing与key-spacing规则冲突问题

解决ESLint @stylistic/key-spacing与@stylistic/type-annotation-spacing的冲突问题

直接修改@stylistic/key-spacing的规则配置,通过overrides选项排除类型注解场景,让它仅作用于对象键值对:

// eslint.config.js
import parserTs from '@typescript-eslint/parser'
import stylistic from '@stylistic/eslint-plugin'

export default [
  {
    files: ["**/*.ts"],
    plugins: {
      '@stylistic': stylistic
    },
    languageOptions: {
      parser: parserTs
    },
    rules: {
      '@stylistic/type-annotation-spacing': ['error', {
        "before": true,
        "after": true
      }],
      '@stylistic/key-spacing': ['error', {
        "beforeColon": false,
        "afterColon": true,
        "mode": "strict",
        // 添加overrides单独配置类型注解的冒号规则
        "overrides": {
          "colon": {
            "before": true,
            "after": true
          }
        }
      }]
    }
  }
];

配置说明

  • overrides.colon会针对类型注解中的冒号单独设置规则,和@stylistic/type-annotation-spacing的配置保持一致,彻底避免冲突。
  • 原有的beforeColon和afterColon参数依然会作用于对象键值对的冒号,完全匹配你期望的格式要求。

配置生效后:

  • 类型注解会保留const foo : string的格式(冒号前后各1个空格)
  • 对象键值对会保持key: 'value'的格式(冒号前无空格,后1个空格)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 19:58:15