I am creating a typescript declaration file for a library written in JavaScript, for this task i am experiencing one problem over and over again. The JavaScript Library in question repeats the following pattern in a number of places.
const commandList = [
"run-like-nuts",
"some-more-commands",
"another-nice-command",
"power-commmand",
// lots more commands like this
// in this list
];
function commandRunner(...args) {
// Does some internal work based on ...args
// returns type Promise<string>
}
commandList.forEach(command => {
Object.defineProperty(commandRunner, command, {
value: function(command, ...args) {
return commandRunner(command, ...args);
}
));
});
How can i define types for the commandRunner function so that when I make use of it in typescript it is callable and it has properties in the commandList, which as is evident from the above example also become callable.
By attempt thus far given my limited grasp of typescript has been the following attempt but this does not resolve the problem. How can i resolve this ?
type ArgsType: string | Object;
type commandList = [
"run-like-nuts",
"some-more-commands",
"another-nice-command",
"power-commmand",
// lots more commands like this
// in this list
];
export default (function commandRunner(...args: ArgsType[]) {}) & {
[command: Command]: (command: Command,...args: ArgsType[]) => Promise<string>
}