-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdemo.ts
64 lines (60 loc) · 1.77 KB
/
demo.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
import { PathAutocomplete } from "./autocomplete";
import { Interpret } from "./interpreter";
import { Parser } from "./parser";
import { Tokenize } from "./tokenizer";
type Run<Obj, T extends PathAutocomplete<Obj>> = Interpret<
Obj,
Parser<Tokenize<T & string>>
>;
const obj = {
comments: [
{
id: 1,
content: "Hello world",
user: { name: "Anurag Hazra", login: "anuraghazra" },
},
{
id: 2,
content: "This is comment",
user: { name: "Anurag Hazra", login: "anuraghazra" },
},
{
id: 3,
content: "TypeScript is awesome!",
user: { name: "John Doe", login: "johndoe" },
},
],
nested: {
elements: {
work: {
too: { data: [1, 2, 3] },
},
},
},
mixed: [
{
data: [
["nested array 1", "nested array 2"],
["nested array 3", "nested array 4"],
],
},
],
} as const;
type Demo1 = Run<typeof obj, "comments[0]">;
type Demo2 = Run<typeof obj, "comments[]">;
type Demo3 = Run<typeof obj, "comments[].$where(id:3)">;
type Demo4 = Run<typeof obj, "comments[].user">;
type Demo5 = Run<typeof obj, "comments[].user.name">;
type Demo6 = Run<typeof obj, "comments[].user.login">;
type Demo7 = Run<typeof obj, "nested.elements.work.too.data[]">;
type Demo8 = Run<typeof obj, "mixed[0].data[0][1]">;
type Demo9 = Run<typeof obj, "mixed[0].data[].[1]">;
// Demo of syntax errors
type DemoInvalidDot1 = Run<typeof obj, "a.">;
type DemoInvalidDot2 = Run<typeof obj, ".">;
type DemoInvalidDot3 = Run<typeof obj, "..">;
type DemoInvalidArray1 = Run<typeof obj, "a[">;
type DemoInvalidArray4 = Run<typeof obj, "a[0]]">;
type DemoInvalidWhere1 = Run<typeof obj, "a.$where">;
type DemoNestedErrors2 = Run<typeof obj, "a.b[].]">;
type DemoNestedErrors3 = Run<typeof obj, "a.b.c.d..">;