npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-typescript-enum

v2.1.0

Published

ESLint rules for TypeScript enums.

Downloads

100,716

Readme

eslint-plugin-typescript-enum

ESLint rules for TypeScript enums.

Motivations

From TypeScript Handbook on Enums:

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript.

In other words, TypeScript enums have their corresponsing runtime representations, they are not erased from your emitted JavaScript files after being compiled. This conflicts with one of the TypeScript Design Non-goals:

Provide additional runtime functionality or libraries. Instead, use TypeScript to describe existing libraries.

Having this TypeScript feature extending into your compiled JavaScript also conflicts with the TypeScript slogan of being a typed superset of JavaScript, which further introduces vendor lock-in.

Orta Therox from Typescript team mentioned in one of his YouTube videos that the TypeScript team actually regrets some of the changes it made in the beginning, including introducing enums which basically add features to JavaScript.

Moreover, using enums in TypeScript has a lot of caveats and edge cases to keep in mind. Some aspects of it are even considered not type safe!!! Head over to these wonderful articles for more details on these issues:

  • https://maxheiber.medium.com/alternatives-to-typescript-enums-50e4c16600b1
  • https://stackoverflow.com/questions/40275832/typescript-has-unions-so-are-enums-redundant/60041791#60041791

Additionally, if you have been using @babel/plugin-transform-typescript, you might have already noticed that one of the important caveats is to avoid using const enums, as those require type information to compile.

Nonetheless, TypeScript is undoubtedly a very fantastic programming language with an extremely powerful type system. Enums may have been a very good feature to have back in the early days (2011) when good alternatives did not yet exist.

We now already have much better alternatives than enums, such as const assertions, string unions, discriminated union, etc. Which is why I created this ESLint plugin to provide you with some handy configs and rules to disallow the use of TypeScript enums.

This article provides a very in-depth exploration on the alternatives to TypeScript enums:

  • https://2ality.com/2020/02/enum-alternatives-typescript.html

Last but not least, as stated in Objects vs Enums section of TypeScript Handbook on Enums:

In modern TypeScript, you may not need an enum when an object with as const could suffice.

The biggest argument in favour of this format over TypeScript’s enum is that it keeps your codebase aligned with the state of JavaScript, and when/if enums are added to JavaScript then you can move to the additional syntax.

Installation

First, install the peer dependencies:

npm install --save-dev eslint typescript @typescript-eslint/parser

Next, install this package:

npm install --save-dev eslint-plugin-typescript-enum

Usage

Recommended Config

Configure this plugin in your ESLint configuration file (.eslintrc.js), this will disallow the use of enums altogether:

module.exports = {
  parser: "@typescript-eslint/parser",
  plugins: ["typescript-enum"],
  extends: ["plugin:typescript-enum/recommended"],
};

Babel Config (Not Recommended)

If you are using @babel/plugin-transform-typescript and want to allow the general use of enums except const enums:

module.exports = {
  parser: "@typescript-eslint/parser",
  plugins: ["typescript-enum"],
  extends: ["plugin:typescript-enum/babel"],
};

Rules

Key: :heavy_check_mark: = recommended, :wrench: = fixable, :thought_balloon: = requires type information

| Name | Description | :heavy_check_mark: | :wrench: | :thought_balloon: | | -------------------------------------------------------------- | -------------------------------------- | ------------------ | -------- | ----------------- | | typescript-enum/no-const-enum | Disallow TypeScript const enums | | | | | typescript-enum/no-enum | Disallow all types of TypeScript enums | :heavy_check_mark: | | |

Contributing

Feel free to suggest new configs and rules, PRs are also welcomed!