Skip to content

AdametherzLab/runtime-type-check

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI TypeScript License: MIT

runtime-type-check

Generate zero-dependency runtime validators from TypeScript interfaces, type aliases, and enums.

Features

  • 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

Installation

bash

bun

bun add @adametherzlab/runtime-type-check

npm

npm install --save-dev @adametherzlab/runtime-type-check

Usage

CLI

bash

Generate validators from TypeScript source files

bun run runtime-type-check --input src/types.ts --output ./generated

With JSDoc comments and strict null checks

bun run runtime-type-check --input src/types.ts --output ./generated --include-jsdoc --strict-null-checks

Watch mode — regenerate on file changes

bun run runtime-type-check --input src/types.ts --output ./generated --watch

Programmatic API

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);

Using Generated Validators

// 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); }

API Reference

extractFromFiles(filePaths: string[]): Promise<ExtractedType[]>

Parse TypeScript source files and extract interfaces, type aliases, and enums.

  • Throws TypeError if filePaths is not an array
  • Throws Error if filePaths is empty
  • Throws Error if any file path does not exist

generateValidators(types: ExtractedType[], options: GeneratorOptions): string

Generate TypeScript validator source code from extracted type definitions.

  • Throws TypeError if types is not an array

GeneratorOptions

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

Testing

bash bun test

License

MIT

About

Generates lightweight runtime validators directly from your TypeScript interfaces with zero dependencies and no build step

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors