forked from ts-essentials/ts-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (30 loc) · 1.08 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
import { Prettify } from "../prettify";
// TODO: merge to ExtractFromArray
type ExtractFromObject<Obj extends Record<PropertyKey, unknown>, Key> = Key extends keyof Obj
? Obj[Key]
: Key extends keyof NonNullable<Obj>
? NonNullable<Obj>[Key] | undefined
: undefined;
// TODO: merge to ExtractFromObject
type ExtractFromArray<Arr extends readonly any[], Key> = any[] extends Arr
? Arr extends readonly (infer T)[]
? T | undefined
: undefined
: Key extends keyof Arr
? Arr[Key]
: undefined;
type GetWithArray<Type, Path, PrettifiedType = Prettify<Type>> = Path extends []
? Type
: Path extends [infer Key, ...infer Rest]
? PrettifiedType extends Record<PropertyKey, unknown>
? GetWithArray<ExtractFromObject<PrettifiedType, Key>, Rest>
: Type extends readonly any[]
? GetWithArray<ExtractFromArray<Type, Key>, Rest>
: undefined
: never;
type Path<Type> = Type extends `${infer Key}.${infer Rest}`
? [Key, ...Path<Rest>]
: Type extends `${infer Key}`
? [Key]
: [];
export type PathValue<Type, StringPath> = GetWithArray<Type, Path<StringPath>>;