ts-reflection
is a TypeScript transformer - it inspects the types and generates runtime code based on them. It is compatible with rollup, webpack and ttypescript projects and works nicely with jest.
You will first need to install ts-reflection
using npm
, yarn
or similar:
# NPM
npm install --dev ts-reflection
# Yarn
yarn add -D ts-reflection
In order to enable ts-reflection
in your Webpack project you need to configure ts-loader
or awesome-typescript-loader
in you Webpack config.
// Using ES6 imports
import transformer from 'ts-reflection/transformer';
// Or using the old syntax
const transformer = require('ts-reflection/transformer').default;
{
test: /\.ts(x)?$/,
loader: 'ts-loader', // Or 'awesome-typescript-loader'
options: {
getCustomTransformers: program => ({
before: [transformer(program)],
}),
},
}
In order to enable ts-reflection
in your Rollup project you need to configure ts-loader
or awesome-typescript-loader
in you rollup config.
import transformer from 'ts-reflection/transformer';
import ts from '@wessberg/rollup-plugin-ts';
// ...
ts({
transformers: [
({ program }) => ({
before: transformer(program),
}),
],
}),
import typescript from 'rollup-plugin-typescript2';
// ...
typescript({
transformers: [
service => ({
before: [transformer(service.getProgram())],
after: [],
}),
],
}),
# NPM
npm install --dev ttypescript
# Yarn
yarn add -D ttypescript
In order to enable ts-reflection
in your TTypescript project you need to configure plugins in your tsconfig.json
.
{
"compilerOptions": {
"plugins": [
{ "transform": "ts-reflection/transformer" }
]
}
}
In order to enable ts-reflection
in your Jest tests you need to switch to ttypescript
compiler.
In your jest.config.js
(or package.json
):
module.exports = {
preset: 'ts-jest',
globals: {
'ts-jest': {
compiler: 'ttypescript',
},
},
};
Either using command line:
$ ts-node --compiler ttypescript ...
Or the programmatic API:
require('ts-node').register({
compiler: 'ttypescript'
})