Typescript, combine object types of tuple elements

You can capture the parameters in a tuple type using a generic type parameter with a constraint of any[] for a rest parameter. You can the use UnionToIntersection described here to merge all tuple item types into a single intersection type

type UnionToIntersection<U> = 
  (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never
function tuple<T extends any[]>(...t: T) {
  let newob = {} as UnionToIntersection<T[number]>;
  for (let ob of t) {
    Object.assign(newob, ob);
  }
  return newob;
}

Playground Link

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top