【发布时间】: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