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 🙏

© 2026 – Pkg Stats / Ryan Hefner

enum8

v1.0.6

Published

The way to create enums in JavaScript and TypeScript

Readme

enum8

A modern, type-safe way to create and work with enums in JavaScript and TypeScript. Create enums from arrays or objects with full type safety and immutability, with powerful type inference capabilities.

Installation

npm install enum8
# or
yarn add enum8
# or
pnpm add enum8

Features

  • 🔒 Type-safe enums for TypeScript
  • 🚀 Zero dependencies
  • 📦 Lightweight and tree-shakeable
  • 🧊 Immutable (Object.freeze)
  • 💪 Advanced type inference with infer property
  • 🎯 Compile-time type checking

Usage

import { enumerate } from 'enum8';

// Create an enum from an array (auto-indexed)
const Colors = enumerate(['RED', 'GREEN', 'BLUE']);
// Type: { readonly RED: 0; readonly GREEN: 1; readonly BLUE: 2; infer: 0 | 1 | 2 }
const color: typeof Colors.infer = Colors.RED; // Type-safe: only 0, 1, or 2 allowed

// Create an enum from an object with number values
const Priorities = enumerate({
  LOW: 0,
  MEDIUM: 1,
  HIGH: 2,
});
// Type: { readonly LOW: 0; readonly MEDIUM: 1; readonly HIGH: 2; infer: 0 | 1 | 2 }
const priority: typeof Priorities.infer = Priorities.HIGH; // Type-safe: only 0, 1, or 2 allowed

// Create an enum from an object with string values
const HttpMethods = enumerate({
  GET: 'GET',
  POST: 'POST',
  PUT: 'PUT',
  DELETE: 'DELETE',
});
// Type: { readonly GET: "GET"; readonly POST: "POST"; readonly PUT: "PUT"; readonly DELETE: "DELETE"; infer: "GET" | "POST" | "PUT" | "DELETE" }
const method: typeof HttpMethods.infer = HttpMethods.GET; // Type-safe: only "GET", "POST", "PUT", "DELETE" allowed

// All enums are immutable
HttpMethods.GET = 'INVALID'; // Error: Cannot assign to 'GET' because it is a read-only property

// Type inference in functions
function handleRequest(method: typeof HttpMethods.infer) {
  // TypeScript knows method can only be "GET", "POST", "PUT", or "DELETE"
  switch (method) {
    case HttpMethods.GET:
      return 'Handling GET request';
    case HttpMethods.POST:
      return 'Handling POST request';
    // ... TypeScript will ensure all cases are handled
  }
}

API Reference

enumerate(definition)

Creates an immutable enum object with full TypeScript type inference.

Parameters

One of the following:

  • string[]: Array of enum keys (values will be numeric indices)
  • Record<string, number>: Object with string keys and number values
  • Record<string, string>: Object with string keys and string values

Returns

Returns a readonly enum object with an additional infer property for type inference:

  • For array input: { readonly [key: string]: number; infer: number }
  • For object input: { readonly [key: string]: T; infer: T } where T is the value type

Type Safety Features

The function provides advanced TypeScript type inference through the infer property:

const Colors = enumerate(['RED', 'GREEN', 'BLUE']);
type ColorValues = typeof Colors.infer; // type: 0 | 1 | 2

const Methods = enumerate({
  GET: 'GET',
  POST: 'POST'
});
type HttpMethod = typeof Methods.infer; // type: "GET" | "POST"

// Type-safe function parameters
function paint(color: typeof Colors.infer) {
  // TypeScript ensures only valid color values can be passed
}

// Compile-time errors for invalid values
paint(Colors.RED); // OK
paint(3); // Error: Argument of type '3' is not assignable...

Why enum8?

  • Type Safety: Unlike TypeScript's built-in enums, enum8 provides true type safety with no runtime overhead
  • Value Inference: The infer property makes it easy to use enum values in type annotations
  • Immutability: All enums are automatically frozen, preventing accidental modifications
  • Simplicity: No class instances or complex methods, just plain immutable objects
  • Tree Shakeable: Works perfectly with modern bundlers for optimal dead code elimination

License

MIT © Dominic Vonk

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.