Home > Enterprise >  Supporting multiple properties on a functions inside a `.d.ts` file
Supporting multiple properties on a functions inside a `.d.ts` file

Time:07-10

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>
}

CodePudding user response:

You should keep the commandList as an array so we can use it both at runtime and at compile time. To make the string literals available to use for the typing, we have to define it with as const.

const commandList = [
  "run-like-nuts",
  "some-more-commands",
  "another-nice-command",
  "power-commmand",
  // lots more commands like this
  // in this list 
] as const;

For the type of commandRunner we can map over the string literals in commandList.

type CommandRunner = ((...args: ArgsType[]) => Promise<string>) & { 
  [Command in typeof commandList[number]]: 
    (command: Command,...args: ArgsType[]) => Promise<string> 
}

Playground

  • Related