TypeScript Transformer for Runtime Types & Reflection
npm install tst-reflect-transformertypescript
import { getType, Type } from "tst-reflect";
function printClassInfo()
{
const type: Type = getType(); // <<== get type of generic TType
console.log(type.name); // > Animal
console.log(type.fullName); // > @@this/index.ts:Animal#21869
console.log(type.getProperties().map(p => ${p.name}: ${p.type.name}).join("\n")); // > name: string
console.log(type.getMethods().map(m => ${m.name}(): ${m.returnType.name}).join("\n")); // > makeSound(): string
return type.name;
}
abstract class Animal
{
private name: string;
abstract makeSound(): string;
}
printClassInfo();
`
How to Start
1. Install packages.
`
npm i tst-reflect && npm i tst-reflect-transformer -D
`
2. In order to use transformer plugin you need TypeScript compiler which supports plugins eg. package ttypescript or you can use TypeScript compiler API manually.
`
npm i ttypescript -D
`
3. Add transformer to tsconfig.json
`json5
{
"compilerOptions": {
// your options...
// ADD THIS!
"plugins": [
{
"transform": "tst-reflect-transformer"
}
]
}
}
`
4. Now just transpile your code by ttsc instead of tsc
`
npx ttsc
`
$3
Modify your webpack config. Use options.compiler of ts-loader to set ttypescript compiler.
`javascript
({
test: /\.(ts|tsx)$/,
loader: require.resolve("ts-loader"),
options: {
compiler: "ttypescript"
}
})
`
$3
Install Parcel plugin.
`
npm i parcel-plugin-ttypescript
`
$3
Install Rollup plugin
`
npm i rollup-plugin-typescript2
`
and modify your rollup config.
`javascript
import ttypescript from "ttypescript";
import tsPlugin from "rollup-plugin-typescript2";
export default {
// your options...
plugins: [
// ADD THIS!
tsPlugin({
typescript: ttypescript
})
]
}
`
$3
Modify your tsconfig.json.
`json5
{
"compilerOptions": {
// your options...
"plugins": [
{
"transform": "tst-reflect-transformer"
}
]
},
// ADD THIS!
"ts-node": {
// This can be omitted when using ts-patch
"compiler": "ttypescript"
},
}
`
Obtaining Type
Runtime package (tst-reflect) contains two main exports, getType function and Type class. To get Type instance, you have to call getType.
How does it work
Transformer looks for all calls of getType and replace those calls by Type retrieving logic. It generates object literals describing referred types and instances of Type
are created from those objects.
$3
Mentioned object literals describing types are called metadata. Default behavior collect metadata of all used types and generate file metadata.lib.js in project root (
location of tsconfig.json).
Metadata library file looks like this:
`javascript
var {getType} = require("tst-reflect");
getType({
k: 5,
props: [{n: "foo", t: getType({n: "string", k: 2})}, {
n: "bar",
t: getType({k: 3, types: [getType({k: 6, v: "a"}), getType({k: 6, v: "b"})], union: true, inter: false})
}]
}, 22974);
getType({k: 5, props: [{n: "foo", t: getType({n: "string", k: 2})}, {n: "bar", t: getType({n: "string", k: 2})}]}, 22969);
getType({
n: "SomeType",
fn: "..\\logger.ts:SomeType",
props: [{n: "array", t: getType({k: 4, n: "Array", args: [getType(22969)]})}],
ctors: [{params: []}],
k: 1,
ctor: () => SomeType
}, 22965);
getType({
n: "Foo",
fn: "..\\logger.ts:Foo",
props: [{n: "prop", t: getType({n: "number", k: 2})}],
ctors: [{params: [{n: "prop", t: getType({n: "number", k: 2})}]}],
k: 1,
ctor: () => Foo
}, 22976);
``