typescript-rtti
raw JSON → 0.9.6 verified Sat May 09 auth: no javascript
A TypeScript transformer and runtime reflection library that emits comprehensive runtime type information (RTTI) for classes, interfaces, functions, and call sites. Current stable version is 0.9.6 (release candidate quality). Requires TypeScript 4.8–5.1 and reflect-metadata. Differentiators: supports emission for a wide range of TypeScript concepts including mapped types (partial), ES modules and CommonJS, isomorphic across browser and Node.js, and compatible with existing design:* metadata. Alternates (e.g., tst-reflect) may lack breadth of type support or require heavier compiler customizations.
Common errors
error Error: reflect-metadata is not defined ↓
cause reflect-metadata polyfill not imported before typescript-rtti.
fix
Add import 'reflect-metadata' at the top of your entry file.
error Reflection metadata not emitted: no type data for class ↓
cause TypeScript transformer is not configured or running.
fix
Add the typescript-rtti transformer to your tsconfig.json compilerOptions.plugins.
error Argument of type 'typeof A' is not assignable to parameter of type 'ReflectableClass' ↓
cause reflect() expects a class constructor, not an instance.
fix
Pass the class itself: reflect(A) not reflect(new A()).
Warnings
deprecated The reflect function signature may change in future stable versions. ↓
fix Pin to ^0.9.6 and watch changelog for breaking changes.
breaking TypeScript version must be exactly 4.8, 4.9, 5.0, or 5.1. Using other versions may cause build failures or incorrect metadata. ↓
fix Ensure TypeScript version matches one of the peer dependencies.
gotcha Mapped types (e.g., Partial<T>) are not supported and may produce no or incomplete metadata. ↓
fix Avoid using mapped types with reflect() or verify behavior per issue tracker.
gotcha require() style CommonJS imports may not work; the package is primarily ESM. ↓
fix Use import syntax. If using CommonJS, use dynamic import or ensure bundler configuration.
Install
npm install typescript-rtti yarn add typescript-rtti pnpm add typescript-rtti Imports
- reflect wrong
import { Reflect } from 'typescript-rtti'correctimport { reflect } from 'typescript-rtti' - CallSite wrong
import { callsite } from 'typescript-rtti'correctimport { CallSite } from 'typescript-rtti' - Reflector wrong
import Reflector from 'typescript-rtti'correctimport { Reflector } from 'typescript-rtti'
Quickstart
import 'reflect-metadata';
import { reflect } from 'typescript-rtti';
class User {
id!: number;
username?: string;
protected favoriteColor?: number | string;
doIt() { return 123; }
}
const reflectedUser = reflect(User);
console.log('favoriteColor is union:', reflectedUser.getProperty('favoriteColor').type.is('union')); // true
console.log('doIt return type is Number:', reflectedUser.getMethod('doIt').type.isClass(Number)); // true