Home > front end >  Is there any way to get intersection type of generic rest parameters?
Is there any way to get intersection type of generic rest parameters?

Time:12-28

I write a merge function that merge multi objects and return.

type A = { a: number };
type B = { b: number };
type C = { c: number };

const a: A = { a: 1 };
const b: B = { b: 2 };
const c: C = { c: 3 };

function merge<T extends any[]>(...args: T): { [k in keyof T]: T[k] } {
    return args.reduce((previous, current) => {
        return Object.assign(previous, current);
    });
}

const m = merge(a, b, c);

m.a;
m.b;
m.c;

What I expect for type of m is A & B & C, but I got [A, B, C] in compiler, and it give me the error.

Property 'a' does not exist on type '[A, B, C]'.ts(2339)

Is there a right way to declare the return type of my merge function?

CodePudding user response:

T[number] will produce a union of the types in the args array, and then you can use a Union to Instersection type to merge the members of that union into an intersection:

type UnionToIntersection<U> = 
  (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never

function merge<T extends any[]>(...args: T): UnionToIntersection<T[number]> {
    return args.reduce((previous, current) => {
        return Object.assign(previous, current);
    });
}

Playground

CodePudding user response:

What you need is an intersection of the elements of the array:

TS Playground

type ArrayIntersection<T extends readonly unknown[]> = T extends [infer Head, ...infer Rest] ?
  Head & ArrayIntersection<Rest>
  : unknown;

function merge <T extends readonly any[]>(...args: T): ArrayIntersection<T> {
  return args.reduce((previous, current) => {
    return Object.assign(previous, current);
  });
}

type A = { a: number };
type B = { b: number };
type C = { c: number };

declare const a: A;
declare const b: B;
declare const c: C;

const m = merge(a, b, c); // A & B & C

m.a; // number
m.b; // number
m.c; // number

Note that intersecting types which are incompatible (or have members whose types are incompatible) will result in never for that type:

declare const result: ArrayIntersection<[
  { a: string },
  { b: number },
  { person: { first: string } },
  { person: { last: string } },
  { b: string },
]>;

result.person; // { first: string; last: string; }
result.a; // string
result.b; // never
  • Related