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

typedecode

v1.0.3

Published

Type-safe runtime validation for TypeScript — parse untrusted input with precise inferred types

Downloads

474

Readme

typedecode

Version 1.0.3 — Type-safe runtime validation for TypeScript.

Turn untrusted data into typed values you can trust. Define a schema once, get full TypeScript inference, and validate at runtime with clear error messages.


Install

npm install typedecode

Requirement: enable strict mode in your tsconfig.json for correct type inference.

{
  "compilerOptions": {
    "strict": true
  }
}

Quick start

import { array, isoDate, number, object, optional, string } from 'typedecode';

const externalData = {
  id: 123,
  name: 'Alison Roberts',
  createdAt: '2026-01-11T12:26:37.024Z',
  tags: ['foo', 'bar'],
};

const userSchema = object({
  id: number,
  name: string,
  createdAt: optional(isoDate),
  tags: array(string),
});

const user = userSchema.verify(externalData);
// Inferred type:
// {
//   id: number;
//   name: string;
//   createdAt?: Date;
//   tags: string[];
// }

Core concepts

Schemas

Every validator in typedecode is a schema — a reusable description of the shape you expect. Built-in schemas cover strings, numbers, booleans, dates, arrays, objects, unions, and more.

Three ways to validate

| Method | Behavior | |--------|----------| | .verify(input) | Returns the typed value, or throws a Validation error | | .decode(input) | Returns { ok: true, value } or { ok: false, error } — never throws | | .value(input) | Returns the typed value, or undefined on failure |

Compose and extend

Chain, transform, refine, and pipe schemas to build complex validators from simple ones:

import { define, email, string } from 'typedecode';

const username = string
  .refine((s) => s.length >= 3, 'Username must be at least 3 characters')
  .transform((s) => s.toLowerCase());

const contact = string.pipe((s) => (s.includes('@') ? email : username));

Custom schemas

Use define() when you need full control:

import { define } from 'typedecode';

const evenNumber = define((blob, ok, err) =>
  typeof blob === 'number' && blob % 2 === 0
    ? ok(blob)
    : err('Must be an even number'),
);

Standard Schema

typedecode schemas implement the Standard Schema interface via the ~standard property, so they work with ecosystem tools that support it.


Built-in schemas

| Category | Schemas | |----------|---------| | Primitives | string, number, integer, boolean, bigint, date | | Strings | email, url, uuid, regex, nonEmptyString, identifier | | Collections | array, tuple, record, object, exact, inexact | | Unions | either, oneOf, enum_, taggedUnion, select | | Modifiers | optional, nullable, nullish, constant, unknown | | JSON | json, jsonObject, jsonArray |

Import everything from the package root:

import { number, object, string } from 'typedecode';

Type inference

Extract the output type of any schema with SchemaType:

import type { SchemaType } from 'typedecode';
import { object, string } from 'typedecode';

const person = object({ name: string });
type Person = SchemaType<typeof person>; // { name: string }

Use isSchema() to check whether a value is a typedecode schema at runtime.


Error formatting

Customize validation errors with built-in formatters or your own:

import { formatInline, formatShort } from 'typedecode';

userSchema.verify(badData, formatShort);

License

MIT