1 | import type {OmitIndexSignature} from './omit-index-signature';
|
2 | import type {PickIndexSignature} from './pick-index-signature';
|
3 | import type {Simplify} from './simplify';
|
4 |
|
5 | // Merges two objects without worrying about index signatures.
|
6 | type SimpleMerge<Destination, Source> = {
|
7 | [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
8 | } & Source;
|
9 |
|
10 | /**
|
11 | Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
12 |
|
13 | @example
|
14 | ```
|
15 | import type {Merge} from 'type-fest';
|
16 |
|
17 | interface Foo {
|
18 | [x: string]: unknown;
|
19 | [x: number]: unknown;
|
20 | foo: string;
|
21 | bar: symbol;
|
22 | }
|
23 |
|
24 | type Bar = {
|
25 | [x: number]: number;
|
26 | [x: symbol]: unknown;
|
27 | bar: Date;
|
28 | baz: boolean;
|
29 | };
|
30 |
|
31 | export type FooBar = Merge<Foo, Bar>;
|
32 | // => {
|
33 | // [x: string]: unknown;
|
34 | // [x: number]: number;
|
35 | // [x: symbol]: unknown;
|
36 | // foo: string;
|
37 | // bar: Date;
|
38 | // baz: boolean;
|
39 | // }
|
40 | ```
|
41 |
|
42 | @category Object
|
43 | */
|
44 | export type Merge<Destination, Source> =
|
45 | Simplify<
|
46 | SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
47 | & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
48 | >;
|