Search code examples
typescripteslinttypescript-eslint

Can typescript-eslint naming-convention allow numbers?


I've recently updated my @typescript-eslint/eslint-plugin and @typescript-eslint/parser from 5.9.1 to 5.38.1, and it's led my eslint . to begin complaining that purely-numeric object indices aren't allowed in my code. Is there some way to configure my ESLint to enforce a convention but not exclude numbers?

EDIT: the warning seems to only appear when numerical indices are not in square brackets, as in the example below. So the in-code fix seems easy enough - just add square brackets to any constants - but it would be good if ESLint reacted consistently!

My .eslintrc contains the following:

'@typescript-eslint/naming-convention': ['warn', {
    'selector': 'property',
    'format': ['strictCamelCase']
}],

I would like to be able to have an object (say, in a test file) like:

{
    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
}

The above numeric indices give the following error:

warning  Object Literal Property name `3` must match one of the following formats: strictCamelCase  @typescript-eslint/naming-convention

Solution

  • I was able to get the expected results using the filter property described in the rule.

    '@typescript-eslint/naming-convention': ['warn', {
        'selector': 'property',
        'format': ['strictCamelCase'],
        'filter': { 'regex': '\\d+', 'match': false }
    }],
    

    DEMO

    The idea is to exclude property names that consist only of digits from being checked against the camel case format. The pattern '\\d+' matches digit-only identifiers as required.