-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
80 lines (70 loc) · 1.85 KB
/
index.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
// The value of T[S], if it's not null/undefined
export type Prop<T, S extends keyof T> = NonNullable<T[S]>;
export function get<
T,
S1 extends keyof NonNullable<T>,
S2 extends keyof Prop<NonNullable<T>, S1>,
S3 extends keyof Prop<Prop<NonNullable<T>, S1>, S2>,
S4 extends keyof Prop<Prop<Prop<NonNullable<T>, S1>, S2>, S3>,
S5 extends keyof Prop<Prop<Prop<Prop<NonNullable<T>, S1>, S2>, S3>, S4>
>(
obj: T,
prop1: S1,
prop2: S2,
prop3: S3,
prop4: S4,
prop5: S5,
): Prop<Prop<Prop<Prop<NonNullable<T>, S1>, S2>, S3>, S4>[S5] | undefined;
export function get<
T,
S1 extends keyof NonNullable<T>,
S2 extends keyof Prop<NonNullable<T>, S1>,
S3 extends keyof Prop<Prop<NonNullable<T>, S1>, S2>,
S4 extends keyof Prop<Prop<Prop<NonNullable<T>, S1>, S2>, S3>
>(
obj: T,
prop1: S1,
prop2: S2,
prop3: S3,
prop4: S4,
): Prop<Prop<Prop<NonNullable<T>, S1>, S2>, S3>[S4] | undefined;
export function get<
T,
S1 extends keyof NonNullable<T>,
S2 extends keyof Prop<NonNullable<T>, S1>,
S3 extends keyof Prop<Prop<NonNullable<T>, S1>, S2>
>(
obj: T,
prop1: S1,
prop2: S2,
prop3: S3,
): Prop<Prop<NonNullable<T>, S1>, S2>[S3] | undefined;
export function get<
T,
S1 extends keyof NonNullable<T>,
S2 extends keyof Prop<NonNullable<T>, S1>
>(
obj: T,
prop1: S1,
prop2: S2
): Prop<NonNullable<T>, S1>[S2] | undefined;
export function get<
T,
S1 extends keyof NonNullable<T>
>(
obj: T,
prop1: S1
): NonNullable<T>[S1] | undefined;
export function get<T>(
obj: T,
...props: string[]
): any | undefined {
let value: any = obj;
while (props.length > 0) {
if (value == null) return undefined;
let nextProp = <keyof any> props.shift();
value = value[nextProp];
}
return value;
}
export default get;