-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedium-replacekeys.ts
More file actions
79 lines (66 loc) · 1.4 KB
/
medium-replacekeys.ts
File metadata and controls
79 lines (66 loc) · 1.4 KB
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
74
75
76
77
78
79
/* _____________ Your Code Here _____________ */
type ReplaceKeys<U, T, Y> = U extends unknown
? {
[i in keyof U]: i extends T ? (i extends keyof Y ? Y[i] : never) : U[i];
}
: U;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/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>>,
];
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/1130/answer
> View solutions: https://tsch.js.org/1130/solutions
> More Challenges: https://tsch.js.org
*/