-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
208 lines (170 loc) · 4.45 KB
/
types.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
type oldTree = {
type: 'div',
children: [
{
type: 'span',
children: [
{
type: 'a'
}
]
},
{
type: 'span',
children: [
{
innerText: 'hello'
}
]
}
]
}
type commands = [
{
type: 'addChild',
parent: ['div', 'span'],
child: {
innerText: 'hello'
}
},
{
type: 'addChild',
parent: ['div'],
child: {
type: 'p',
value: 'i am content'
}
}
]
type Tree = oldTree
type Commands = commands
type JSONTree = {
div: ['div'] & {
span: ['div', 'span'] & {
a: ['div', 'span', 'a']
}
}
}
type Parent = JSONTree['div']['span']
type ParentPath = Parent[number]
// test case 1
type Path = ['a', 'b']
type MyObj = {
a: {
b: {
value: string
}
}
d: {}
}
type MyObjKeys = keyof MyObj
type PickByPath<T extends Record<string, any>, X> = X extends keyof T ? T[X] : never
type Shift<T> = T extends [infer First, ...infer Rest] ? Rest : never
type First<T> = T extends [infer First, ...infer Rest] ? First : never
type PickByPathArr<T extends Record<string, any>, Paths> =
Paths extends [infer First, ...infer Rest]
? Rest extends []
? PickByPath<T, First>
: PickByPathArr<PickByPath<T, First>, Rest>
: never
type v = PickByPathArr<MyObj, ['a', 'b']>
type v2 = PickByPathArr<Tree, ['children', 0, 'children']>
// test case 2
type MyObj2 = {
type: 'div',
children: [
{
type: 'span',
children: []
}
]
}
type MyCmd = { op: 'addChild', parent: ['div', 'span'], child: { type: 'p'} }
type MyCmd2 = { op: 'removeChild', parent: ['div', 'span'], child: { type: 'p'} }
type MyCmd3 = { op: 'replaceChild', parent: ['div', 'span'], child: { type: 'p'} }
interface MyObjTree {
type: 'div' | 'span' | 'p',
children?: MyObjTree[]
}
interface Command {
op: 'addChild' | 'removeChild' | 'replaceChild',
parent: MyObjTree['type'][]
child: MyObjTree
}
type Assign<U, T> = Omit<U, keyof T> & T
type PickSecond<T extends any[]> = T extends [infer First, infer Second, ...any[]] ? [Second] : never
type v33 = PickSecond<[string, 2]>
type RemoveItem<Children extends MyObjTree['children'], child extends MyObjTree> =
Children extends [infer First, ...infer Rest]
? First extends child
? Rest extends []
? []
: Rest extends MyObjTree['children'] ? RemoveItem<Rest, child> : []
: Rest extends MyObjTree['children'] ? [First, ...RemoveItem<Rest, child>] : []
: []
type DoCommand<T extends MyObjTree, Cmd extends Command> =
Cmd['op'] extends 'addChild'
? Assign<T, { children: [...T['children'], Cmd['child']] }>
: Cmd['op'] extends 'removeChild'
? Assign<T, { children: RemoveItem<T['children'], Cmd['child']> }>
: Cmd['op'] extends 'replaceChild'
? Cmd['child']
: never
type PatchToChildren<T extends MyObjTree['children'], Cmd extends Command> =
T extends [infer FirstChild, ...infer RestChildren]
? FirstChild extends MyObjTree
? RestChildren extends MyObjTree['children']
? [PatchChildToObj<FirstChild, Cmd>, ...PatchToChildren<RestChildren, Cmd>]
: never
: never
: T
type V22Cmd = { op: 'addChild', parent: ['span'], child: { type: 'p'} }
type V22Obj = [
{
type: 'span',
children: []
},
{
type: 'span',
children: []
}
]
type v22 = PatchToChildren<V22Obj, V22Cmd >
type v22c = v22[1]['children']
type PatchChildToObj<T extends MyObjTree, Cmd extends Command> =
Cmd['parent'] extends [infer First, ...infer Rest]
? Rest extends []
? T extends { type: First }
? DoCommand<T, Cmd>
: never
: Rest extends Command['parent']
? Assign<T, { children: PatchToChildren<T['children'], Assign<Cmd, { parent: Rest }>> }>
: never
: T;
type v3 = PatchChildToObj<MyObj2, MyCmd>
type c1 = ShowResult<v3>
type v4 = PatchChildToObj<v3, MyCmd2>
type c2 = ShowResult<v4>
type v5 = PatchChildToObj<v4, MyCmd3>
type c3 = ShowResult<v5>
type ShowResult<T> = {
[P in keyof T]:
T[P] extends [infer First, ...infer Rest]
? [ {
[P2 in keyof First]: First[P2]
}, ...ShowResult<Rest>]
: T[P] extends object
? {
[P2 in keyof T[P]]: T[P][P2]
}
: T[P]
}
type v5s = ShowResult<v4>
type A1 = { children: ['string'] }
type A2 = { children: ['string', 'number'] }
type A3 = Assign<A1, A2>
type A4 = A3['children']
function a () {
return 1 as const
}
const v = a() // not as const here