0

This function must support any number of arguments:

type Result<T extends object[]> = "???"

function merge<T extends object[]>(...sources: T): Result<T> {
  return Object.assign({}, ...sources)
}

Example input with expected result type: (playground)

type Expected = {
  a: 2
  b: 1 | 2
  c?: 1
  d?: 1 | 2
  e: 2
  f: 2
}

// The return type should match `Expected` exactly. No intersections please!
const result: Expected = merge(
  {} as {
    a: 1
    b: 1
    c?: 1
    d?: 1
    e?: 1
  },
  {} as {
    a: 2
    b?: 2
    d?: 2
    e: 2
    f: 2
  }
)

Related question: Typescript, merge object types?

2 Answers 2

2
type Result<T extends object[]> = UnionToIntersection<T[number]>

/**
 * @author https://stackoverflow.com/users/2887218/jcalz
 * @see https://stackoverflow.com/a/50375286/10325032
 */
type UnionToIntersection<Union> =
  (Union extends any
    ? (argument: Union) => void
    : never
  ) extends (argument: infer Intersection) => void
      ? Intersection
      : never;

See TypeScript Playground.

2
  • Ack, I should have been clearer. I actually used exactly that trick at first, but I would prefer to not have the intersection in the result type. I want it the result variable (in the example) to be exactly the Expected type. Sorry!
    – aleclarson
    Mar 7, 2019 at 16:14
  • 1
    No worries! I've just noticed this solution is flawed anyway. The inferred type is very different than Expected and if requires an explicit type definition. Mar 7, 2019 at 16:18
2

UPDATE for TS4.1+

TypeScript 4.1 has (finally!) introduced support for recursive conditional types, so now you can write SpreadTuple for arbitrary-length tuples, like this:

type SpreadTuple<T extends readonly any[]> =
  T extends [infer F] ? F :
  T extends [infer F, ...infer R] ?
  Spread<F, SpreadTuple<R>> : never;

(this also uses variadic tuple types introduced in TS4.0 to avoid the cumbersome Tail definition).

Soooo much nicer than the hoops you had to jump through before. You can verify that this works in the original example code:

const result: Expected = merge(
  {} as {
    a: 1
    b: 1
    c?: 1
    d?: 1
    e?: 1
  },
  {} as {
    a: 2
    b?: 2
    d?: 2
    e: 2
    f: 2
  }
); // okay

const r = merge({ a: 1, b: 2 }, { b: "3", c: "4" }, { c: true, d: false });
// {  a: number;  b: string;  c: boolean;  d: boolean; }

Playground link to code


Pre-TS4.1 answer:

The short answer is that you can't do it for an arbitrary number of values. The slightly longer answer is that you shouldn't try to do it, since the natural recursive definition will not work and the various ways you can trick the compiler into doing it are officially frowned upon.

If you are willing to support up to some reasonable but finite maximum number of arguments, you can do it. Indeed, the standard library definition of Object.assign() currently is just a few overloads with intersections. And though this might change sometime, it seems to work well enough for people.

Assuming we take Spread<L, R> from the other answer as a starting point, we can make our own SpreadTuple that works for anything up to a fixed length:

type Tail<L extends any[]> =
  ((...l: L) => void) extends ((h: infer H, ...t: infer T) => void) ? T : never;

type SpreadTuple<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple1<Tail<T>>>
type SpreadTuple1<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple2<Tail<T>>>
type SpreadTuple2<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple3<Tail<T>>>
type SpreadTuple3<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple4<Tail<T>>>
type SpreadTuple4<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple5<Tail<T>>>
type SpreadTuple5<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple6<Tail<T>>>
type SpreadTuple6<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple7<Tail<T>>>
type SpreadTuple7<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple8<Tail<T>>>
type SpreadTuple8<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTuple9<Tail<T>>>
type SpreadTuple9<T extends {}[], L extends number = T['length']> = L extends 0 ? never : L extends 1 ? T[0] : Spread<T[0], SpreadTupleX<Tail<T>>>
type SpreadTupleX<T extends {}[]> = T[number]; // give up

I did it that way so you can see how to easily extend it to any length you care about. You can do it without Tail if you're comfortable hardcoding a lot of Spread<Spread<Spread<....>>> around.

Anyway, now this works:

// use default parameter R to expand result to easy-to-digest type
function merge<T extends object[], R = SpreadTuple<T>>(...sources: T): { [K in keyof R]: R[K] } {
  return Object.assign({}, ...sources);
}

const result: Expected = merge(
  {} as {
    a: 1
    b: 1
    c?: 1
    d?: 1
    e?: 1
  },
  {} as {
    a: 2
    b?: 2
    d?: 2
    e: 2
    f: 2
  }
)
//const result: {
//  c?: 1 | undefined;
//  a: 2;
//  e: 2;
//  f: 2;
//  b: 1 | 2;
//  d: 1 | 2 | undefined;
//}

And let's just try one with more than two arguments:

const r = merge({ a: 1, b: 2 }, { b: "3", c: "4" }, { c: true, d: false });
// {  a: number;  b: string;  c: boolean;  d: boolean; }

Looks good to me.

Hope that helps. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.