Validation300K+/wkMIT

runtypes

Runtypes is a runtime validation library for TypeScript that provides a simple, composable API for defining types that are enforced at runtime. Runtypes creates

Installation

npm
npm install runtypes
yarn
yarn add runtypes
pnpm
pnpm add runtypes

Import

ESM
import { Record, String, Number, Optional, Static } from 'runtypes';

Quick Example

usage
import { Record, String, Number, Optional, Static } from 'runtypes';

const User = Record({
  name: String,
  email: String,
  age: Optional(Number),
});

type User = Static<typeof User>;

const user = User.check({ name: 'Alice', email: '[email protected]' });
console.log(user.name);

About runtypes

Runtypes is a runtime validation library for TypeScript that provides a simple, composable API for defining types that are enforced at runtime. Runtypes creates type guards and validators from declarative type definitions that mirror TypeScript's type syntax. The library provides primitive types (String, Number, Boolean), compound types (Record, Array, Tuple, Union, Intersect), utility types (Optional, Nullable, Undefined), and literal types for creating schemas. Runtypes uses a contract-based approach where types can check values (.check() throws on invalid data), guard values (.guard() returns boolean), and validate values (.validate() returns result). The Static<typeof MyType> utility extracts the TypeScript type from a Runtype definition. Runtypes supports branded types for creating nominal types (types that are structurally identical but semantically different, like UserId vs. PostId), which prevent accidental mixing of different ID types. The library also supports constraint types through .withConstraint() for adding custom validation logic. Runtypes is lighter than Zod and io-ts while providing similar core functionality. It is particularly useful for validating API responses, configuration objects, and function inputs where you need both runtime safety and TypeScript type inference.

Quick Facts

Packageruntypes
CategoryValidation
Weekly Downloads300K+
LicenseMIT
Installnpm install runtypes

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.