为 typescript 接口标识符关闭 eslint camelCase

Turn off eslint camelCase for typescript interface identifiers

我正在尝试将现有的 JS 存储库迁移到 TypeScript。问题是项目的 API 响应是在 snake_case 中具有标识符的对象,这与默认的 eslint camelCase 规则冲突。

在迁移之前,我们在 .eslintrc.js:

中使用此规则处理 snake_case 个对象标识符
'camelcase': ['error', {properties: 'never'}],

并且可以在我们的代码中包含这样的对象:

const { variable_name } = await axios(url)

但是在迁移到 TypeScript 之后,这些标识符也会出现在界面中。

所以基本上我希望能够拥有:

interface Model<T extends string> {
    type: T;
    id: number;
    variable_name: string;
    language_code: string;
}

但是 Typescript 现在抛出:

Identifier 'variable_name' is not in camel case.

我的 .eslintrc.js 文件中应该有什么规则来证明这一点?请注意,我的标识符不限于上面列出的这两个,还有许多其他标识符我为简单起见删除了。

请记住 JavaScript(为其开发 ESLint)不知道类型(例如接口)。

所以我认为这就是 linting 文件时发生的事情:

  • ESLint 的 camelCase 规则适用于对象及其属性。
  • ESLint 的解析器不理解你有一个接口,并认为那些是另一个块中的变量。因此检查会导致错误的结果。

我认为你需要做的是:

  • 禁用 camelCase 规则(至少对于您的 TypeScript 文件)。
  • 改用 naming-convention 规则并根据需要进行配置。

这应该有效:

"@typescript-eslint/naming-convention": [
    "error",
    {
        "selector": "default",
        "format": ["camelCase"]
    },
    {
        "selector": "typeProperty",
        "format": null
    }
]