Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Supporting multiple properties on a functions inside a `.d.ts` file

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!