TypeScript transformer for code generation.
See also Interface-based instructions.
npm i --save-dev tx-reflector
getInterfaces(target: Function | object): string[]
getInterfaces<T>(target: T): string[]
getRawInterfaces<T>(target: T): Interface[]
getComponentInterfaces(XClass: Function): string[]
import {getInterfaces, getRawInterfaces, Interface} from 'tx-reflector';
const data: IData = {value: 'foo'};
const interfaces: string[] = getInterfaces(data); // OR getInterfaces<IData>(anything);
const rawInterfaces: Interface[] = getRawInterfaces(data);;
// After compilation:
// var interfaces = ["IData", "IAbstractData"];
// var rawInterfaces = [
// {
// name: "IData",
// entries: [{name: "value", type: "string", optional: false}],
// }, {
// name: "IAbstractData",
// entries: [{name: "source", type: "string", optional: false}],
// },
// ];
import {getComponentInterfaces} from 'tx-reflector';
interface IBtnProps extends IComponent, IClickable {
value: string;
}
class Btn extends React.Component<IBtnProps> {
}
const interfaces = getComponentInterfaces(Btn);
// After compilation:
// var interfaces = ["IBtnProps", "IComponent", "IClickable"];
Use awesome-typescript-loader or ts-loader 2.3+.
// webpack.config.js
const {default:txReflector} = require('tx-reflector/src/transformer/transformer');
module.exports = {
// ...
module: {
// ...
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader', // or ts-loader
options: {
getCustomTransformers() {
return {
before: [txReflector],
after: [],
};
}
}
},
},
// ...
};
// .jest/tsPreprocessor.js
const tsc = require('typescript');
const tsConfig = require('../tsconfig.json');
const {default:txReflector} = require('tx-reflector/src/transformer/transformer');
module.exports = {
process(src, path) {
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
const result = tsc.transpileModule(src, {
compilerOptions: tsConfig.compilerOptions,
fileName: path,
transformers: {
before: [txReflector],
after: [],
},
});
return result.outputText;
}
return src;
},
};
npm i
npm test
, code coverage