首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打字稿-禁止命名-惯例能允许编号吗?

打字稿-禁止命名-惯例能允许编号吗?
EN

Stack Overflow用户
提问于 2022-09-30 14:02:11
回答 1查看 203关注 0票数 1

我最近已经将我的@typescript-eslint/eslint-plugin@typescript-eslint/parser5.9.1更新到了5.38.1,这使得我的eslint .开始抱怨代码中不允许使用纯数字对象索引。是否有办法将我的ESLint配置为强制执行约定,但不排除数字?

编辑:如下面的例子所示,只有当数值指数不在方括号内时,才会出现警告。因此,代码中的修复看起来很容易--只需在任何常量中添加方括号--但是如果ESLint一致地做出反应,那就更好了!

我的.eslintrc包含以下内容:

代码语言:javascript
复制
'@typescript-eslint/naming-convention': ['warn', {
    'selector': 'property',
    'format': ['strictCamelCase']
}],

我希望能够有一个对象(例如,在测试文件中),例如:

代码语言:javascript
复制
{
    0: true,         // should be accepted; currently raises a warning
    8: false,        // should be accepted; currently raises a warning
    [12]: true,      // accepted
    "foo": "bar",    // accepted
    "foo-bar": "baz" // should cause a warning because not strictCamelCase
}

上面的数字索引给出了以下错误:

代码语言:javascript
复制
warning  Object Literal Property name `3` must match one of the following formats: strictCamelCase  @typescript-eslint/naming-convention
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-04 10:52:34

我能够使用规则中描述的filter属性获得预期的结果。

代码语言:javascript
复制
'@typescript-eslint/naming-convention': ['warn', {
    'selector': 'property',
    'format': ['strictCamelCase'],
    'filter': { 'regex': '\\d+', 'match': false }
}],

这样做的目的是将仅由数字组成的属性名称排除在根据camel case格式进行检查之外。模式'\\d+'与所需的仅数字标识符匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73909962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档