rttist
v1.0.0-beta.8
Published
Abstract declarations for TypeScript runtime reflection
Downloads
285
Maintainers
Readme
What is RTTIST?
RTTIST (pronounced /ˈɑː(r)tɪst/, like artist) is a TypeScript runtime reflection system inspired by the C#'s reflection.
It consists of several parts:
- Metadata Generator - TypeGen - a standalone command-line tool that generates metadata from TypeScript types.
- Runtime Library - a library that provides runtime reflection functionality.
- Transformers - a set of transformers that extend the functionality of the TypeScript build/bundle tools.
To access base functionality, you only need to install the TypeGen and the runtime library. The transformers are optional. They are only needed if you want to use the extended functionality. TypeGen is a standalone noninvasive command-line tool. It does not depend on your project setup; it is completely decoupled from your build/bundle tool.
Here is a small example:
import { Type } from "rttist";
import { Metadata } from "./metadata.typelib"; // this file is generated by TypeGen
const typeIController: Type = getType<IController>();
const controllerCtors = Promise.all(
Metadata.getTypes()
.filter(
(type) =>
type.isClass() &&
!type.abstract &&
type.exported &&
type.isDerivedFrom(typeIController)
)
.map((type) => type.module.import().then(m => m?.[type.name]))
);
const controllerInstances = controllerCtors.map(Ctor => new Ctor());
In this example, we get all exported non-abstract classes that are derived from the IController
interface and create instances of them.
Module metadata.typelib
is generated by the TypeGen. It exports Metadata
object which is instance of the MetadataLibrary
. It contains metadata for all types and modules in the project.
It is useful for many things, such as:
- serialization/deserialization,
- validation,
- dependency injection,
- frameworks and libraries.
For more information see our website rttist.org and docs docs.rttist.org.
Supported Build/Bundle Tools
Thanks to RTTIST's flexible architecture, it seamlessly integrates with a variety of build and bundle tools.
However, it's important to note that certain build and bundle tools may come with limitations, hindering the full breadth of RTTIST's capabilities. For the ultimate experience, you have to use the TypeScript compiler directly. Unlike other tools, the TypeScript compiler's TypeChecker empowers us to reflect inferred types, unlocking a deeper level of code understanding.
While we provide custom plugins for the most widely used tools, you have the flexibility to create your own if necessary. We provide TS to TS compiler (npm: @rttist/ts-loader-wasm), implemented in Rust, that can be used in any plugin for any build/bundle tool in a loader.
For now, we provide plugins for:
- esbuild,
- Vite (work in progress - ~90 %),
- SWC (work in progress - ~90 %),
- ttypescript (work in progress).
Alpha Warning!
This project is currently under development.
There may still be major changes.
If you have any issues or questions or just want to give us feedback, join our Discord.
Showcase
import { getType } from "./metadata.typelib";
import { Type, PropertyInfo, MethodInfo } from "rttist";
interface Employee {
name: string;
salary: number;
sayHello();
sayHello(toSomebody: string);
}
const type: Type = getType<Employee>();
if (type.isInterface()) {
const properties = type.getProperties().map((prop: PropertyInfo) => prop.name);
const methods = type.getMethods().map((method: MethodInfo) => method.name);
console.log(properties); // > [ name, salary ]
console.log(methods); // > [ sayHello ]
const sayHelloMethod: MethodInfo = type.getMethods().find(m => m.name === "sayHello");
const signatures = sayHelloMethod.getSignatures()
.map(sig => {
const parameters = sig.getParameters()
.map(param => param.name + ": " + param.type.name);
return `sayHello(${parameters.join(", ")})`
});
console.log(signatures); // > [ sayHello(), sayHello(toSomebody: string) ]
}
License
This project is licensed under the MIT license.