Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda