-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path599-merge.ts
50 lines (45 loc) · 836 Bytes
/
599-merge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 599 - Merge
*
* Merge two types into a new type. Keys of the second type overrides keys of the first type.
*
* For example
*
* ```ts
* type foo = {
* name: string
* age: string
* }
* type coo = {
* age: number
* sex: string
* }
*
* type Result = Merge<foo, coo> // expected to be {name: string, age: number, sex: string}
* ```
*/
/* _____________ Your Code Here _____________ */
type Merge<F, S> = {
[K in (keyof F | keyof S)]: K extends keyof S
? S[K]
: K extends keyof F
? F[K]
: never;
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type Foo = {
a: number
b: string
}
type Bar = {
b: number
c: boolean
}
type cases = [
Expect<Equal<Merge<Foo, Bar>, {
a: number
b: number
c: boolean
}>>,
]