UNPKG

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