There isn't a specific guideline for this situation within the core ESLint or typescript-eslint modules. I checked for any plugins created by the community, but couldn't find one.
In the meantime, you can partially address this issue using the built-in rule no-restricted-syntax
. This rule can be customized to target specific ESQuery selectors. For example, you can configure it to identify and flag variable declarations that lack type annotations and initialize an empty array:
"no-restricted-syntax": [
"error",
{
"message": "Don't use evolving any arrays.",
"selector": "VariableDeclarator:matches([id.typeAnnotation=undefined]):matches([init.type=ArrayExpression]):matches([init.elements.length=0])"
}
]
Here's a typescript-eslint playground demonstrating this in action. It may be possible to refine the selector for better precision.
An alternative approach would involve creating a dedicated lint rule specifically for this scenario. This would eliminate the need to handle no-restricted-syntax
settings, which can become complex when dealing with ESLint configuration overrides, and could provide automated fixes/suggestions as well.
I suggest considering submitting an issue on typescript-eslint. Upon searching, I didn't come across any existing discussions related to arrays without type annotations or other similar topics. Checking for duplicate issues using different search terms could help ensure no prior discussions exist.
If this enhancement doesn't get implemented as a plugin rule in typescript-eslint, there's always the option of developing your own custom ESLint rule and publishing it as a standalone plugin. The typescript-eslint.io > Custom Rules documentation provides guidance on working with the tooling for custom rules.