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

enumkit

v0.1.0

Published

Zero-dependency TypeScript enum utilities: enumValues, fromValue, fromName, isEnumValue, exhaustive switch check, bit-flag helpers. Works with 'as const' const objects.

Downloads

152

Readme

enumkit

All Contributors

Zero-dependency TypeScript enum utilities for as const objects: enumValues, fromValue, fromName, isEnumValue, exhaustive switch check, bit-flag helpers. Port of Python enum.Enum / Java enum reflection / C# Enum.Parse.

npm License: MIT

The problem

TypeScript's built-in enum has well-known footguns (numeric enums, const enum erasure, module augmentation issues). The idiomatic alternative is as const:

const Status = { Active: 'active', Inactive: 'inactive', Pending: 'pending' } as const;
type StatusValue = typeof Status[keyof typeof Status]; // 'active' | 'inactive' | 'pending'

But then you lose everything Python enum.Enum / Java enum / C# Enum gives you for free: iteration, reverse lookup, type-safe exhaustiveness checks, runtime validation.

enumkit adds all of that back, with full TypeScript inference.

Install

npm install enumkit

Quick start

import {
  enumValues, enumKeys, enumEntries,
  isEnumValue, fromValue, fromName,
  createEnum, exhaustive,
  hasFlag, addFlag, flagsSet
} from "enumkit";

const Status = { Active: 'active', Inactive: 'inactive', Pending: 'pending' } as const;

enumValues(Status)          // ['active', 'inactive', 'pending']
enumKeys(Status)            // ['Active', 'Inactive', 'Pending']
isEnumValue(Status, 'active')   // true  (narrows type)
fromValue(Status, 'active')     // 'Active'   ← like Java Enum.valueOf()
fromName(Status, 'Active')      // 'active'   ← like Python MyEnum['Active'].value

API

Core functions

enumValues<T>(obj: T): T[keyof T][]
enumKeys<T>(obj: T): (keyof T & string)[]
enumEntries<T>(obj: T): [keyof T & string, T[keyof T]][]
enumSize<T>(obj: T): number

isEnumValue<T>(obj: T, value: unknown): value is T[keyof T]   // type guard
isEnumKey<T>(obj: T, key: unknown): key is keyof T & string   // type guard

fromValue<T>(obj: T, value: T[keyof T]): keyof T | undefined  // reverse lookup
fromValueOrThrow<T>(obj: T, value: T[keyof T]): keyof T       // throws EnumError
fromName<T>(obj: T, key: string): T[keyof T] | undefined      // forward lookup
fromNameOrThrow<T>(obj: T, key: string): T[keyof T]           // throws EnumError

createEnum — OOP API

const Status$ = createEnum({ Active: 'active', Inactive: 'inactive' } as const);

Status$.values()                // ['active', 'inactive']
Status$.keys()                  // ['Active', 'Inactive']
Status$.entries()               // [['Active', 'active'], ...]
Status$.size                    // 2
Status$.isValue('active')       // true
Status$.isKey('Active')         // true
Status$.fromValue('active')     // 'Active'
Status$.fromName('Active')      // 'active'
Status$.fromNameOrThrow('X')    // throws EnumError

// Iterable
for (const [key, value] of Status$) { ... }

exhaustive — switch exhaustiveness

import { exhaustive } from "enumkit";

type Color = 'red' | 'green' | 'blue';

function hex(c: Color): string {
  switch (c) {
    case 'red':   return '#ff0000';
    case 'green': return '#00ff00';
    case 'blue':  return '#0000ff';
    default: return exhaustive(c); // TypeScript error if any case is missing
  }
}

If you add 'yellow' to the Color type but forget the case 'yellow': branch, TypeScript shows an error at the exhaustive(c) call site.

Bit-flag utilities

For numeric enums acting as bit flags (like C# [Flags] / Java EnumSet):

import { hasFlag, hasAnyFlag, hasAllFlags, addFlag, removeFlag, toggleFlag, flagsSet, combineFlags } from "enumkit";

const Permission = { Read: 1, Write: 2, Execute: 4 } as const;

const rw = combineFlags(Permission.Read, Permission.Write);  // 3
hasFlag(rw, Permission.Read)     // true
hasFlag(rw, Permission.Execute)  // false
hasAnyFlag(rw, Permission.Write, Permission.Execute)  // true
hasAllFlags(rw, Permission.Read, Permission.Write)    // true

addFlag(rw, Permission.Execute)    // 7
removeFlag(rw, Permission.Write)   // 1
toggleFlag(rw, Permission.Execute) // 7

flagsSet(rw, Permission)  // ['Read', 'Write']

Comparison

| Feature | Python enum.Enum | Java enum | C# Enum | enumkit | |---|---|---|---|---| | Iteration | list(MyEnum) | MyEnum.values() | Enum.GetValues() | enumValues(obj) | | Value → name | MyEnum(val).name | via loop | n/a | fromValue(obj, val) | | Name → value | MyEnum['name'].value | MyEnum.valueOf(name) | Enum.Parse() | fromName(obj, name) | | Type guard | isinstance | instanceof | n/a | isEnumValue(obj, val) | | Exhaustive check | match + assert_never | sealed (Java 17+) | n/a | exhaustive(val) | | Bit flags | Flag subclass | EnumSet | [Flags] | hasFlag/addFlag/... |

Why not ts-enum-util?

ts-enum-util (2022, ~12k/week) works with TypeScript's native enum keyword — which has known issues. enumkit works with the idiomatic as const pattern, gives you zero-dep runtime utilities with full generic inference, and adds exhaustive checking + bit flags in one package.

Contributors ✨

This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

License

MIT © trananhtung