Skip to content

Commit b0a606b

Browse files
authored
Merge: Improve documentation (#1375)
1 parent 76bd9cd commit b0a606b

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

source/merge.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ type SimpleMerge<Destination, Source> = Simplify<{
1212
/**
1313
Merge two types into a new type. Keys of the second type overrides keys of the first type.
1414
15+
This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
16+
17+
@example
18+
```
19+
import type {Merge} from 'type-fest';
20+
21+
type Foo = {
22+
a: string;
23+
b: number;
24+
};
25+
26+
type Bar = {
27+
a: number; // Conflicts with Foo['a']
28+
c: boolean;
29+
};
30+
31+
// With `&`, `a` becomes `string & number` which is `never`. Not what you want.
32+
type WithIntersection = (Foo & Bar)['a'];
33+
//=> never
34+
35+
// With `Merge`, `a` is cleanly overridden to `number`.
36+
type WithMerge = Merge<Foo, Bar>['a'];
37+
//=> number
38+
```
39+
1540
@example
1641
```
1742
import type {Merge} from 'type-fest';

0 commit comments

Comments
 (0)