-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01130-medium-replacekeys.ts
73 lines (62 loc) · 1.21 KB
/
01130-medium-replacekeys.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type NodeA = {
type: 'A'
name: string
flag: number
}
type NodeB = {
type: 'B'
id: number
flag: number
}
type NodeC = {
type: 'C'
name: string
flag: number
}
type ReplacedNodeA = {
type: 'A'
name: number
flag: string
}
type ReplacedNodeB = {
type: 'B'
id: number
flag: string
}
type ReplacedNodeC = {
type: 'C'
name: number
flag: string
}
type NoNameNodeA = {
type: 'A'
flag: number
name: never
}
type NoNameNodeC = {
type: 'C'
flag: number
name: never
}
type Nodes = NodeA | NodeB | NodeC
type ReplacedNodes = ReplacedNodeA | ReplacedNodeB | ReplacedNodeC
type NodesNoName = NoNameNodeA | NoNameNodeC | NodeB
type cases = [
Expect<
Equal<
ReplaceKeys<Nodes, 'name' | 'flag', { name: number; flag: string }>,
ReplacedNodes
>
>,
Expect<Equal<ReplaceKeys<Nodes, 'name', { aa: number }>, NodesNoName>>
]
// ============= Your Code Here =============
type ReplaceKeys<Input, SearchKeys, Updater> = {
[InputKeys in keyof Input]: InputKeys extends SearchKeys
? InputKeys extends keyof Updater
? Updater[InputKeys]
: never
: Input[InputKeys]
}