Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda