'ESLint rules for no-unused-enums

There's a no-unused-vars eslint rule that can warn/error if a variable is unused: https://eslint.org/docs/rules/no-unused-vars

If I want to ensure that there are no unused enums, what would be the best lint rule to implement?

Example scenario:

export enum MyEnum {
  ONE = 'ONE',
  TWO = 'TWO',
  THREE = 'THREE',
}

Here MyEnum.ONE and MyEnum.TWO are both used, but MyEnum.THREE is not referenced.


if (type === MyEnum.ONE) {
  ...
}

if (type === MyEnum.TWO) {
  ...
}

It would be great if a lint rule could warn/error that MyEnum.THREE is unused so that it may be removed.



Solution 1:[1]

There is no existing rule to do this so you've got a couple of options:

  1. Write your own rule (and publish it for the rest of us to use!)
  2. If are you IntelliJ, then there is an inspection in the IDE ("Unused global symbol") that can give you the warning.

As an aside, make sure you are using the Typescript version of no-unused-vars rule. Even then it doesn't detect unused exported variables because each file is analysed individually.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 matt helliwell