在特定接口中禁用标识符的驼峰规则

joh*_*pin 7 typescript eslint typescript-eslint

我使用@typescript-eslint插件通过 eslint 将规则添加到我的 TypeScript 代码中。

我启用的规则之一是驼峰命名

"@typescript-eslint/camelcase": ["error", { "properties": "always" }]
Run Code Online (Sandbox Code Playgroud)

它允许我规范我的变量和接口名称。

问题是标准 JSON 格式使用 Snake_case 约定来命名它们的标识符。所以我所有的API请求响应都使用snake_case。

在这种情况下,我必须使用 Snake_case 声明接口,如下所示:

"@typescript-eslint/camelcase": ["error", { "properties": "always" }]
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:有没有办法对接口结尾宽度中定义的标识符禁用此规则Response?所以我的界面中没有 lint 错误UserResponse

@typescript-eslint 提供了一个有趣的参数,允许禁用与正则表达式匹配的特定名称的规则:

interface UserResponse {
  id: number
  dark_mode: boolean
  status_code: string
}
Run Code Online (Sandbox Code Playgroud)

这里 linter 不会触发 Snake_case 格式,Foo_Bar_Response因为它以单词 结尾Response

但这里它仅适用于接口本身的名称,不适用于这些子标识符的名称。

知道我们如何才能做到这一点吗?

Iag*_*eto 0

它是 github 上的一个已解决问题https://github.com/typescript-eslint/typescript-eslint/issues/2077您可以尝试修复不推荐的代码库。您还可以尝试使用以下选项禁用:基于项目的驼峰变量或接口道具的用例https://eslint.org/docs/rules/camelcase