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

149
Views
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
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!