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

如何让@typescript-eslint/naming-convention仅作用于非静态类属性?

解决@typescript-eslint/naming-convention规则匹配范围问题

问题分析

你当前配置的@typescript-eslint/naming-convention规则中,modifiers: ["public"]会匹配所有带public修饰符的类属性——包括同时带static修饰符的静态属性,这就导致NULL_CATEGORY_PLACEHOLDER、EMAIL_CHANNEL这类静态常量也触发了命名错误。而你的需求是:

  • 规则仅作用于非静态的public实例属性(header_name和consentStatement)
  • 仅consentStatement触发错误(header_name符合snake_case格式,consentStatement不符合)

解决方案

调整ESLint规则配置,拆分规则条目区分静态与非静态public属性:

"@typescript-eslint/naming-convention": [
  "error",
  // 非静态public实例属性:要求snake_case格式
  {
    "format": ["snake_case"],
    "modifiers": ["public"],
    "selector": "classProperty",
    "filter": {
      "modifiers": ["static"],
      "match": false
    }
  },
  // 静态public属性:允许UPPER_CASE格式(符合常量命名习惯)
  {
    "format": ["UPPER_CASE"],
    "modifiers": ["public", "static"],
    "selector": "classProperty"
  }
]

配置说明

  • 第一条规则:通过filter选项排除带static修饰符的属性,仅对仅含public修饰符的实例属性生效。header_name符合snake_case无报错,consentStatement因是camelCase触发错误。
  • 第二条规则:专门匹配同时带public和static的静态属性,要求使用UPPER_CASE格式,正好匹配你的静态常量,不会触发错误。

效果验证

应用配置后:

  • NULL_CATEGORY_PLACEHOLDER、EMAIL_CHANNEL:符合UPPER_CASE规则,无错误
  • header_name:符合snake_case规则,无错误
  • consentStatement:不符合snake_case规则,触发错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 00:05:09