-
Notifications
You must be signed in to change notification settings - Fork 12
/
autocomplete.ts
48 lines (44 loc) · 1.43 KB
/
autocomplete.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
// Autocomplete is not ready yet
import { NumberMap } from "./utils";
type ArrayKeys = keyof (readonly [] | any[]);
type NumberKeys = keyof number;
type StringifyObject<T> = T extends any
? keyof {
[K in keyof T as T[K] extends Record<string, any>
? never
: `${K & string}:${Extract<T[K], string | number>}`];
} &
string
: never;
type PathRecursive<T, Key extends keyof T> = Key extends string
? T[Key] extends ReadonlyArray<any> | Array<any>
?
| `${Key}[${Exclude<keyof T[Key], ArrayKeys> & string}]`
| `${Key}[${Exclude<keyof T[Key], ArrayKeys> & string}].${PathRecursive<
T[Key],
Exclude<keyof T[Key], ArrayKeys> & string
>}`
| `${Key}[]`
| `${Key}[].${PathRecursive<
T[Key],
Exclude<keyof T[Key], ArrayKeys> & string
>}`
| `${Key}[].$where(${StringifyObject<T[Key][number]>})`
: Key extends keyof NumberMap
? PathRecursive<
T[Key],
Exclude<keyof T[Key], ArrayKeys | NumberKeys> & string
>
: T[Key] extends Record<string, any>
?
| Key
| `${Key}.${PathRecursive<
T[Key],
Exclude<keyof T[Key], ArrayKeys | NumberKeys> & string
>}`
| `${Key}.${Exclude<keyof T[Key], ArrayKeys | NumberKeys> & string}`
: Key
: never;
export type PathAutocomplete<T> =
| (PathRecursive<T, keyof T> | keyof T)
| (string & {});