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 🙏

© 2025 – Pkg Stats / Ryan Hefner

tyne

v1.0.13

Published

Tyne is a minimalist, type-safe schema system for defining data shapes, inferring TypeScript types, validating runtime data, and generating `.d.ts` files. Built for speed, clarity, and type fidelity.

Downloads

27

Readme

Tyne

“Shape your data. Trust your types.”

Tyne is a minimalist, type-safe schema system for defining data shapes, inferring TypeScript types, validating runtime data, and generating .d.ts files. Built for speed, clarity, and type fidelity.

Features

  • 🚀 Runtime validation with detailed error messages
  • 🔒 Type-safe schemas with automatic TypeScript inference
  • 🧩 Composable validators with chainable API
  • Zero dependencies and lightweight
  • 📝 Type generation for TypeScript definitions

Installation

npm install tyne
# or
yarn add tyne
# or
pnpm add tyne

Basic Usage

import { t } from 'tyne';

// Define a schema
const userSchema = t.object({
  id: t.number(),
  name: t.string(),
  email: t.email().optional(),
  createdAt: t.instanceof(Date),
  tags: t.array(t.union([t.literal('admin'), t.literal('user')])),
});

// Infer TypeScript type
type User = t.infer<typeof userSchema>;
/*
{
  id: number;
  name: string;
  email?: string | undefined;
  createdAt: Date;
  tags: ('admin' | 'user')[];
}
*/

// Validate data
const validUser = {
  id: 1,
  name: 'John Doe',
  createdAt: new Date(),
  tags: ['admin', 'user'],
};

const result = userSchema.validate(validUser); // Returns validated data

// Handle invalid data
try {
  userSchema.validate({ id: '1', name: 123 });
} catch (error) {
  console.error(error.message);
  // "Object validation failed:
  //   - "id": Expected number, got string
  //   - "name": Expected string, got number
  //   - "createdAt": Expected instance of Date, got undefined"
}

Core Validators

Primitive Types

| Validator | Description | TypeScript Equivalent | | --------------- | --------------------- | --------------------- | | t.string() | Validates strings | string | | t.number() | Validates numbers | number | | t.boolean() | Validates booleans | boolean | | t.null() | Validates null | null | | t.undefined() | Validates undefined | undefined | | t.any() | Accepts any value | any |

Complex Types

| Validator | Description | | --------------------------------- | ------------------------------------- | | t.array(type) | Validates arrays of specified type | | t.object(shape) | Validates objects with specific shape | | t.tuple(type1, type2).res(type) | Validates fixed-length arrays | | t.union(type1, type2) | Validates one of several types | | t.literal(value) | Validates specific literal values | | t.instanceof(Class) | Validates class instances |

Specialized Validators

| Validator | Description | | ----------- | ---------------------------------- | | t.email() | Validates email format strings | | t.url() | Validates URL format strings | | t.tel() | Validates telephone number strings |

Type Modifiers

Optional Fields

t.string().optional();
// string | undefined

Default Values

t.number().default(0);
// Returns 0 if undefined

Advanced Features

Custom Validation with .refine()

const positiveNumber = t
  .number()
  .refine((val) => val > 0 || 'Must be positive');
positiveNumber.validate(5); // OK
positiveNumber.validate(-1); // Throws "Must be positive"

Value Transformation with .transform()

const toUpperCase = t.string().transform((str) => str.toUpperCase());
toUpperCase.validate('hello'); // "HELLO"

const toDate = t.string().transform((val) => new Date(val));
toDate.validate('2023-01-01'); // Date object

Complex Object Validation

const passwordSchema = t
  .object({
    password: t.string(),
    confirmPassword: t.string(),
  })
  .refine(
    (data) => data.password === data.confirmPassword || 'Passwords must match',
  );

passwordSchema.validate({
  password: 'secret',
  confirmPassword: 'different',
});
// Throws "Passwords must match"

Extract TypeScript Type

const schema = t.object({
  name: t.string(),
  age: t.number(),
});

type SchemaType = t.infer<typeof schema>;
/*
{
  name: string;
  age: number;
}
*/

Generate TypeScript Definitions

console.log(schema.toDts('UserType'));
/*
export type UserType = {
  name: string;
  age: number;
};
*/

API Reference

Core Methods

| Method | Description | | --------------------- | ----------------------------------------- | | validate(value) | Validate and return value or throw error | | safeValidate(value) | Return validation result without throwing | | toDts(name?) | Generate TypeScript type definition | | optional() | Mark field as optional | | default(value) | Provide default value for optional fields | | refine(validator) | Add custom validation logic | | transform(fn) | Transform value after validation |

Error Handling

Tyne provides detailed error messages:

try {
  userSchema.validate(invalidData);
} catch (error) {
  if (error instanceof Error) {
    console.error(error.message);
    /*
    Object validation failed:
      - "id": Expected number, got string
      - "name": Expected string, got number
      - "createdAt": Expected instance of Date, got undefined
    */
  }
}

Whether you're building libraries, APIs, or tools that rely on strong typing, Tyne helps you keep your data consistent across dev and runtime — without unnecessary weight.

📝 License

This project is licensed under the MIT License – see the LICENSE file for details.

Author: Estarlin R (estarlincito.com)