-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00956-hard-deeppick.ts
55 lines (50 loc) · 1.26 KB
/
00956-hard-deeppick.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
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type Obj = {
a: number
b: string
c: boolean
obj: {
d: number
e: string
f: boolean
obj2: {
g: number
h: string
i: boolean
}
}
obj3: {
j: number
k: string
l: boolean
}
}
type cases = [
Expect<Equal<DeepPick<Obj, ''>, unknown>>,
Expect<Equal<DeepPick<Obj, 'a'>, { a: number }>>,
Expect<Equal<DeepPick<Obj, 'a' | ''>, { a: number } & unknown>>,
Expect<
Equal<DeepPick<Obj, 'a' | 'obj.e'>, { a: number } & { obj: { e: string } }>
>,
Expect<
Equal<
DeepPick<Obj, 'a' | 'obj.e' | 'obj.obj2.i'>,
{ a: number } & { obj: { e: string } } & { obj: { obj2: { i: boolean } } }
>
>
]
// ============= Your Code Here =============
type Get<T, Path> = Path extends `${infer Head}.${infer Tail}`
? Head extends keyof T
? { [P in keyof T as P extends Head ? P : never]: Get<T[Head], Tail> }
: never
: Path extends keyof T
? { [P in keyof T as P extends Path ? P : never]: T[Path] }
: never
type UnionToIntersection<T> = (
T extends unknown ? (arg: T) => unknown : never
) extends (arg: infer R) => unknown
? R
: never
type DeepPick<T, Path extends string> = UnionToIntersection<Get<T, Path>>