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 install runtypes
yarn add runtypes
pnpm add runtypes
Import
import { Record, String, Number, Optional, Static } from 'runtypes';Quick Example
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
| Package | runtypes |
| Category | Validation |
| Weekly Downloads | 300K+ |
| License | MIT |
| Install | npm install runtypes |
Related Packages
Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defin…
io-ts is a runtime type system for TypeScript that bridges the gap between compile-time types and ru…
Superstruct is a library for validating data in JavaScript and TypeScript using composable, predicta…
TypeBox is a JSON Schema type builder that creates in-memory JSON Schema objects that can be statica…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.