-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.ts
33 lines (28 loc) · 966 Bytes
/
get.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
// stolen from https://github.com/ghoullier/awesome-template-literal-types#dot-notation-string-type-safe
export type PathImpl<T, Key extends keyof T> = Key extends string
? T[Key] extends Record<string, any> ?
| `${Key}.${
& PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>>
& string}`
| `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}`
: never
: never;
export type PathImpl2<T> = PathImpl<T, keyof T> | keyof T;
export type Path<T> = PathImpl2<T> extends string | keyof T ? PathImpl2<T>
: keyof T;
export type PathValue<T, P extends Path<T>> = P extends
`${infer Key}.${infer Rest}`
? Key extends keyof T ? Rest extends Path<T[Key]> ? PathValue<T[Key], Rest>
: never
: never
: P extends keyof T ? T[P]
: never;
export default function get<T, P extends Path<T>>(
object: T,
path: P,
): PathValue<T, P> {
return (path as string).split(".").reduce(
(o: any, k: string) => o && o[k],
object,
);
}