$ show fabiandev/ts-runtime
ts-runtime is a developer tool that generates runtime type checks from TypeScript type annotations, transforming TypeScript source code into JavaScript with inserted type assertions. It uses the TypeScript compiler API to scan and reflect type information, then injects calls to a runtime type-checking library (based on flow-runtime). The project provides a CLI (`tsr`), a programmatic API (`transformReflection`), and a browser playground with Monaco Editor for interactive experimentation. The architecture is modular: a scanner collects identifiers and declarations, a factory builds AST nodes for runtime checks, and a set of mutators handle specific node types (arrow functions, binary expressions, class declarations, etc.). The runtime library (`ts-runtime.lib.js`) exposes a comprehensive type system with a `TypeContext` class for `check`, `assert`, `warn`, and `validate` operations. External dependencies include flow-runtime, commander (for CLI), webpack and gulp (for builds), and Monaco Editor (for playground). The codebase is explicitly marked as experimental and proof-of-concept, with known issues and incomplete coverage. It is not production-ready.
The LLM identified 3 files worth highlighting for reuse:
The core runtime type system providing a hierarchy of type classes (AnyType, ObjectType, FunctionType, UnionType, etc.) and a TypeContext class with methods for checking, asserting, warning, and validating values against TypeScript types. This module is self-contained and could be extracted as a standalone runtime type-checker library for any JavaScript project. It demonstrates clean object-oriented design and a comprehensive type model.
A self-contained implementation of a mutator that transforms arrow function expressions to include runtime type checks. It extends a base Mutator class and overrides methods to modify the AST. This pattern is cleanly separated and can be reused as a template for building custom AST transformations for other node types. It demonstrates practical use of the TypeScript compiler API for code generation.
An AST node factory that programmatically builds TypeScript AST nodes representing runtime check calls (e.g., `t.number()`, `t.string()`). It provides helper functions like `libCall` and manages type reflections (ConstructorReflection, ArrayReflection, etc.). This module encapsulates the complexity of constructing runtime check expressions and is reusable for any project that needs to inject similar function calls into TypeScript ASTs.