a-k*_*kon 2 javascript typescript eslint tslint typescript-eslint
我需要对接口或类型内的属性使用 Snake_case。我设定camelcase规则
'camelcase': ["error", {properties: "never"}],
Run Code Online (Sandbox Code Playgroud)
正如文档中提到的。它不适用于接口和类型,但适用于 JS 对象。
export const test = {
a_b: 1, // OK. No error
};
export interface ITest {
a_b: number, // Identifier 'a_b' is not in camel case.eslint(camelcase)
}
export type TTest = {
a_b: number, // Identifier 'a_b' is not in camel case.eslint(camelcase)
}
Run Code Online (Sandbox Code Playgroud)
当我将规则设置为 时,错误消失'off',因此此规则适用于 .ts 文件。
那么如何在TS中使用snake_case呢?谢谢。
我相信该规则不适用于类型,我建议通过在 ESLint 中实现以下块来禁用 TypeScript 文件的此规则。
{
"rules": {
"camelcase": ["error"],
},
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"rules": {
"camelcase": ["off"]
}
}
],
}
Run Code Online (Sandbox Code Playgroud)
一旦我们禁用了该规则,我们就可以应用@typescript-eslint/eslint-plugin额外的 linting 规则,它将正确地对接口和类型中的属性进行 lint 检测。
注意:如果您还没有安装插件和解析器,则必须安装,可以在此处找到说明: https: //www.npmjs.com/package/@typescript-eslint/eslint-plugin
参考@typescript-eslint/naming-convention: https: //github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md
{
"plugins": ["@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"rules": {
"camelcase": ["error"],
"@typescript-eslint/naming-convention": [
"error",
{ "selector": "property", "format": ["snake_case"] }
]
},
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"rules": {
"camelcase": ["off"]
}
}
],
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7093 次 |
| 最近记录: |