I using @typescript-eslint/naming-convention
to enforce my variable names.
I set a rule that allow by default to every variable to be camelCase syntax, except class and object.
But when I use destructuring assignment with import function, I got eslint error that consider the BarClass
as variable and apply the rule of camelCase instead of class rule:
Variable name
BarClass
must match one of the following formats: camelCase 12:11 - 12:19
Is there a way to fix that? How I make eslint to know that is a class and not variable?
typescript-eslint.io playground
class BarClass {
}
const geClass = async() => {
return { BarClass };
}
const bla = true;
(async () => {
const { BarClass } = await geClass();
})();
eslint config:
{
"rules": {
"@typescript-eslint/naming-convention": [
"error",
{
"selector": [
"default"
],
"format": [
"camelCase"
]
},
{
"selector": [
"class", "objectLiteralProperty"
],
"format": null
}
]
}
}
CodePudding user response:
Well, a class is a TypeScript interface overlaying a JavaScript variable. See here
According to this there is no option for that rule to do what you want it to do
CodePudding user response:
const { BarClass } = await geClass();
is the same thing as
const BarClass = (await geClass()).BarClass;
(it's easier to look at it this way).
In this statement, the identifier BarClass
appears two times: the first occurrence of BarClass
is a variable name, the second one is a property name. ESLint is complaining that your variable is not in camel case, which is true. You could fix that by renaming the variable to match the rule conventions, but that would end up make your code less clear:
const { BarClass: barClass } = await geClass();
or equivalently
const barClass = (await geClass()).BarClass;
I'm afraid that the rule you are using has no way to make exceptions for variables with the type of a class, which would be the preferred solution.