interface Comp1 {
someNumber: number;
}
interface Comp2 {
someString: string;
}
const mySystem = (dt: number, myC1: Comp1, myC2: Comp2) => {
console.log('coolio1');
}
function otherSystem(dt: number, otherC1: Comp1, otherC2: Comp2) {
console.log('coolio2');
}
// Array [ "Comp1", "Comp2" ]
console.log(fnParameterTypes(mySystem));
// Array [ "Comp1", "Comp2" ]
console.log(fnParameterTypes(otherSystem));
// Array [ "Comp1", "Comp2" ]
console.log(fnParameterTypes<(dt: number, otherC1: Comp1, otherC2: Comp2) => void>(otherSystem));
Currently it does not work with generics:
function otherSystem(dt: number, otherC1: Comp<C1>, otherC2: Comp<C2>) {
console.log('coolio2');
}
// Array [ "Comp", "Comp" ]
console.log(fnParameterTypes(mySystem));
This is a higly untested and experimental code base, but if you want to try this you can do the following:
run:
npm i -D ts-fn-parameter-type
in you webpack.config.js:
const parameterTypeNamesTransformer = require('ts-fn-parameter-type/transformer').default; // <--
module.exports = ['ts-loader'].map(loader => ({
// ... omitted
module: {
rules: [
{
// ... omitted
options: {
// make sure not to set `transpileOnly: true` here, otherwise it will not work
getCustomTransformers: program => ({
before: [
parameterTypeNamesTransformer(program)
]
})
}
},
],
},
}));
install ttypescript
npm install -D ttypescript
set the transform as a plugin
{
"compilerOptions": {
// ...
"plugins": [
{ "transform": "ts-fn-parameter-type/transformer" },
]
},
// ...
}
use ttsc instead of tsc
read more about it here
Heavily based on ts-transformer-keys