Issue
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?
Solution
What you need is an intersection of the elements of the array:
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
Answered By - jsejcksn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.