forked from preactjs/preact-iso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-match.types.ts
More file actions
103 lines (83 loc) · 2.51 KB
/
pattern-match.types.ts
File metadata and controls
103 lines (83 loc) · 2.51 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Test this file by running:
// npx tsc --noEmit test/node/pattern-match.types.ts
import type { RoutePropsForPath } from '../../src/router.js';
// Test utils
type isEqualsType<T, U> = T extends U ? U extends T ? true : false : false;
type isWeakEqualsType<T, U> = T extends U ? true : false;
// Type tests based on router-match.test.js cases
// Base route test
const test1: isEqualsType<
RoutePropsForPath<'/'> ,
{ params: {} }
> = true;
const test1_1: isEqualsType<
RoutePropsForPath<'/'> ,
{ arbitrary: {} }
> = false;
// Param route test
const test2: isEqualsType<
RoutePropsForPath<'/user/:id'> ,
{ params: { id: string }, id: string }
> = true;
const test2_weak: isWeakEqualsType<
RoutePropsForPath<'/user/:id'> ,
{ params: { id: string } }
> = true;
// Param rest segment test
const test3: isEqualsType<
RoutePropsForPath<'/user/*'> ,
{ params: {}, rest: string }
> = true;
const test3_1: isEqualsType<
RoutePropsForPath<'/*'> ,
{ params: {}, rest: string }
> = true;
const test3_2: isEqualsType<
RoutePropsForPath<'*'> ,
{ params: {}, rest: string }
> = true;
// Param route with rest segment test
const test4: isEqualsType<
RoutePropsForPath<'/user/:id/*'> ,
{ params: { id: string }, id: string, rest: string }
> = true;
// Optional param route test
const test5: isEqualsType<
RoutePropsForPath<'/user/:id?'> ,
{ params: { id?: string }, id?: string }
> = true;
// Optional rest param route "/:x*" test
const test6: isEqualsType<
RoutePropsForPath<'/user/:id*'> ,
{ params: { id?: string }, id?: string }
> = true;
// rest param should not be present
const test6_error: isEqualsType<
RoutePropsForPath<'/user/:id*'> ,
{ params: { id: string }, rest: string }
> = false;
// Rest param route "/:x+" test
const test7: isEqualsType<
RoutePropsForPath<'/user/:id+'> ,
{ params: { id: string }, id: string }
> = true;
// rest param should not be present
const test7_error: isEqualsType<
RoutePropsForPath<'/user/:id+'>,
{ params: { id: string }, id: string, rest: string }
> = false;
// Handles leading/trailing slashes test
const test8: isEqualsType<
RoutePropsForPath<'/about-late/:seg1/:seg2/'> ,
{ params: { seg1: string; seg2: string }, seg1: string, seg2: string }
> = true;
// Multiple params test (from overwrite properties test)
const test9: isEqualsType<
RoutePropsForPath<'/:path/:query'> ,
{ params: { path: string; query: string }, path: string, query: string }
> = true;
// Empty route test
const test10: isEqualsType<
RoutePropsForPath<''> ,
{ params: {} }
> = true;