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

152
Vistas
How to iterate over elements of a generic within a type definition in typescript

I have a function which effectively merges objects together like so

function merge(...objects) {
  let temp = {};
  for(const obj of objects) {
    for(const key in obj) {
      temp[key] = obj[key];
    }
  }
  return temp;
}

What is the correct way to describe the output type such that if for example: objects is of type [{a: number}, {b: string}], the output is of type {a: number, b: string}?

My attempt at this intuitively is as follows:

type _return<T extends any[]> = {
  [name in keyof T]: typeof T[name]
};
function merge<T extends any[]>(...objects: T): _return<T> {
  let temp: _return<T> = {};
  for(const obj of objects) {
    for(const key in obj) {
      temp[key] = obj[key];
    }
  }
  return temp;
}

I know this is incorrect, but maybe it will serve as a better description of my intentions.

I've taken a look at how lodash types merge objects, and it seems even they have given up

Edit: Typos in code

about 4 years ago · Juan Pablo Isaza
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