Skip to main content
Home

Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
Publisheda year ago (1.0.6)

The way to create enums in JavaScript and TypeScript

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.

Built and signed on
GitHub Actions

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@dominicvonk/enum

Import symbol

import * as enum from "@dominicvonk/enum";
or

Import directly with a jsr specifier

import * as enum from "jsr:@dominicvonk/enum";

Add Package

pnpm i jsr:@dominicvonk/enum
or (using pnpm 10.8 or older)
pnpm dlx jsr add @dominicvonk/enum

Import symbol

import * as enum from "@dominicvonk/enum";

Add Package

yarn add jsr:@dominicvonk/enum
or (using Yarn 4.8 or older)
yarn dlx jsr add @dominicvonk/enum

Import symbol

import * as enum from "@dominicvonk/enum";

Add Package

vlt install jsr:@dominicvonk/enum

Import symbol

import * as enum from "@dominicvonk/enum";

Add Package

npx jsr add @dominicvonk/enum

Import symbol

import * as enum from "@dominicvonk/enum";

Add Package

bunx jsr add @dominicvonk/enum

Import symbol

import * as enum from "@dominicvonk/enum";