-
-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge
: Add index signatures support and improve performances (#455)
- Loading branch information
Showing
2 changed files
with
73 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
import type {Except} from './except'; | ||
import type {OmitIndexSignature} from './omit-index-signature'; | ||
import type {PickIndexSignature} from './pick-index-signature'; | ||
import type {Simplify} from './simplify'; | ||
|
||
type Merge_<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType; | ||
|
||
/** | ||
Merge two types into a new type. Keys of the second type overrides keys of the first type. | ||
@example | ||
``` | ||
import type {Merge} from 'type-fest'; | ||
type Foo = { | ||
a: number; | ||
b: string; | ||
}; | ||
interface Foo { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
foo: string; | ||
bar: symbol; | ||
} | ||
type Bar = { | ||
b: number; | ||
[x: number]: number; | ||
[x: symbol]: unknown; | ||
bar: Date; | ||
baz: boolean; | ||
}; | ||
const ab: Merge<Foo, Bar> = {a: 1, b: 2}; | ||
export type FooBar = Merge<Foo, Bar>; | ||
// => { | ||
// [x: string]: unknown; | ||
// [x: number]: number; | ||
// [x: symbol]: unknown; | ||
// foo: string; | ||
// bar: Date; | ||
// baz: boolean; | ||
// } | ||
``` | ||
@category Object | ||
*/ | ||
export type Merge<FirstType, SecondType> = Simplify<Merge_<FirstType, SecondType>>; | ||
export type Merge<Destination, Source> = Simplify<{ | ||
[Key in keyof OmitIndexSignature<Destination & Source>]: Key extends keyof Source | ||
? Source[Key] | ||
: Key extends keyof Destination | ||
? Destination[Key] | ||
: never; | ||
} & PickIndexSignature<Destination & Source>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters