Generate zero-dependency runtime validators from TypeScript interfaces, type aliases, and enums.
- Zero Dependencies — Generated validators use plain TypeScript/JavaScript with no external libraries
- Interface-First — Write standard TypeScript interfaces; validators are generated as build artifacts
- Tiny Bundles — No runtime validation library overhead; tree-shakeable to ~0 bytes when unused
- CLI & Programmatic API — Flexible integration into build pipelines, pre-commit hooks, or custom tooling
- Strict Type Safety — Built with TypeScript strict mode; catches edge cases at generation time
- Enum & Type Alias Support — Extracts and validates enums (including const enums) and union type aliases
bash
bun add @adametherzlab/runtime-type-check
npm install --save-dev @adametherzlab/runtime-type-check
bash
bun run runtime-type-check --input src/types.ts --output ./generated
bun run runtime-type-check --input src/types.ts --output ./generated --include-jsdoc --strict-null-checks
bun run runtime-type-check --input src/types.ts --output ./generated --watch
import { extractFromFiles, generateValidators } from "@adametherzlab/runtime-type-check"; import type { GeneratorOptions } from "@adametherzlab/runtime-type-check";
// Extract types from source files const types = await extractFromFiles(["./src/models/user.ts"]);
// Generate validator code const options: GeneratorOptions = { outputDir: "./generated", includeComments: true, strictMode: true, };
const validatorCode = generateValidators(types, options); console.log(validatorCode);
// Given this interface: export interface User { id: string; email: string; age?: number; }
// The generator produces: import { validateUser, isUser } from "./generated/validators";
const data = JSON.parse(apiResponse); const result = validateUser(data);
if (result.success) { // result.data is typed as User console.log(result.data.email); } else { // result.errors contains validation details console.error(result.errors); }
// Or use the type guard: if (isUser(data)) { // data is narrowed to User console.log(data.email); }
Parse TypeScript source files and extract interfaces, type aliases, and enums.
- Throws
TypeErroriffilePathsis not an array - Throws
ErroriffilePathsis empty - Throws
Errorif any file path does not exist
Generate TypeScript validator source code from extracted type definitions.
- Throws
TypeErroriftypesis not an array
| Option | Type | Default | Description |
|---|---|---|---|
outputDir |
string |
— | Output directory for generated files |
includeComments |
boolean |
false |
Include JSDoc comments in output |
strictMode |
boolean |
false |
Enable strict validation (e.g., NaN rejection) |
fileExtension |
string |
".ts" |
File extension for generated files |
bash bun test
MIT