enumwaii

Getting started

Install enumwaii, declare a vocabulary, and validate your first external value.

Enumwaii is for string vocabularies that cross application boundaries: roles, states, event types, permissions, provider values, and other sets where a raw string is easy to mistype or source from the wrong place.

Install

Use the package manager that owns the application:

npm install enumwaii

The package ships ESM and CommonJS builds with declarations and source maps. Node.js 18 or newer is supported, and the ESM entry point is also tested under Bun, Deno, and Cloudflare Workers without Node compatibility flags. In a Cloudflare Workers project, use whichever package manager already owns the project and import enumwaii normally.

The only runtime dependency is the type-focused official Standard Schema specification package. Zod, Valibot, and ESLint are opt-in.

Declare and extract

import {  } from "enumwaii";

export const  = (["ADMIN", "USER", "GUEST"]);
export const  = .;
export type  = (typeof )["~type"];

Hover an identifier in any TypeScript example in these guides to inspect the same inferred type and API documentation that an editor would show.

Use the extracted member object in application code:

function (: ): boolean {
  return  === .;
}

(.); // valid
("ADMIN"); // TypeScript error
Argument of type '"ADMIN"' is not assignable to parameter of type '("ADMIN" & EnumwaiiBrand<"enumwaii:ADMIN" | "enumwaii:USER" | "enumwaii:GUEST", "ADMIN">) | ("USER" & EnumwaiiBrand<"enumwaii:ADMIN" | "enumwaii:USER" | "enumwaii:GUEST", "USER">) | ("GUEST" & EnumwaiiBrand<...>)'.

Enumwaii deliberately requires this brand. At runtime ROLE.ADMIN is still the ordinary string "ADMIN"; at compile time it proves that the value came from a compatible declaration.

Parse external input

JSON, form fields, route parameters, database rows, and agent output are untrusted even when TypeScript gives their container a convenient shape.

const  = .(.);

const  = .(.("role"));
if (.) {
  (.);
} else {
  (.);
}

Use default for missing values and fallback for any invalid value:

.(, { : . });
.(, { : . });
.(, {
  : .,
  : .,
});

For a nil input, default wins. For any other non-member input, fallback applies. With neither option, parse throws EnumwaiiParseError and safeParse returns its failure branch.

Pass it as a schema

Every declaration implements Standard Schema v1, so compatible libraries can accept it directly:

({ :  });

const  = await ["~standard"].();

If an integration requires a library-specific schema type, use the optional Zod or Valibot adapter.

Continue

On this page