【问题标题】:Disable camelcase rule for identifier in specifics interfaces在特定接口中禁用标识符的驼峰式规则
【发布时间】:2020-07-21 02:04:02
【问题描述】:

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

我启用的规则之一是camelcase

"@typescript-eslint/camelcase": ["error", { "properties": "always" }]

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

问题在于标准 JSON 格式使用蛇形大小写约定来命名它们的标识符。所以我所有的 API 请求响应都使用了 snake_case。

在这种情况下,我必须像这样使用 snake_case 声明接口:

interface UserResponse {
  id: number
  dark_mode: boolean
  status_code: string
}

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

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

/* @typescript-eslint/camelcase: ["error", {allow: ["Response$"]}] */

interface Foo_Bar_Response {
  ...
}

这里 linter 不会触发来自Foo_Bar_Response 的snake_case 格式,因为它以单词Response 结尾。

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

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

【问题讨论】:

    标签: typescript eslint typescript-eslint


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2017-09-05
      • 2015-05-11
      • 2012-05-14
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      相关资源
      最近更新 更多