|
| 1 | +// Test this file by running: |
| 2 | +// npx tsc --noEmit test/node/pattern-match.types.ts |
| 3 | + |
| 4 | +import type { RoutePropsForPath } from '../../src/router.js'; |
| 5 | + |
| 6 | +// Test utils |
| 7 | + |
| 8 | +type isEqualsType<T, U> = T extends U ? U extends T ? true : false : false; |
| 9 | +type isWeakEqualsType<T, U> = T extends U ? true : false; |
| 10 | + |
| 11 | +// Type tests based on router-match.test.js cases |
| 12 | + |
| 13 | +// Base route test |
| 14 | +const test1: isEqualsType< |
| 15 | + RoutePropsForPath<'/'> , |
| 16 | + { params: {} } |
| 17 | +> = true; |
| 18 | + |
| 19 | +const test1_1: isEqualsType< |
| 20 | + RoutePropsForPath<'/'> , |
| 21 | + { arbitrary: {} } |
| 22 | +> = false; |
| 23 | + |
| 24 | +// Param route test |
| 25 | +const test2: isEqualsType< |
| 26 | + RoutePropsForPath<'/user/:id'> , |
| 27 | + { params: { id: string }, id: string } |
| 28 | +> = true; |
| 29 | + |
| 30 | +const test2_weak: isWeakEqualsType< |
| 31 | + RoutePropsForPath<'/user/:id'> , |
| 32 | + { params: { id: string } } |
| 33 | +> = true; |
| 34 | + |
| 35 | +// Param rest segment test |
| 36 | +const test3: isEqualsType< |
| 37 | + RoutePropsForPath<'/user/*'> , |
| 38 | + { params: {}, rest: string } |
| 39 | +> = true; |
| 40 | + |
| 41 | +const test3_1: isEqualsType< |
| 42 | + RoutePropsForPath<'/*'> , |
| 43 | + { params: {}, rest: string } |
| 44 | +> = true; |
| 45 | + |
| 46 | +const test3_2: isEqualsType< |
| 47 | + RoutePropsForPath<'*'> , |
| 48 | + { params: {}, rest: string } |
| 49 | +> = true; |
| 50 | + |
| 51 | +// Param route with rest segment test |
| 52 | +const test4: isEqualsType< |
| 53 | + RoutePropsForPath<'/user/:id/*'> , |
| 54 | + { params: { id: string }, id: string, rest: string } |
| 55 | +> = true; |
| 56 | + |
| 57 | +// Optional param route test |
| 58 | +const test5: isEqualsType< |
| 59 | + RoutePropsForPath<'/user/:id?'> , |
| 60 | + { params: { id?: string }, id?: string } |
| 61 | +> = true; |
| 62 | + |
| 63 | +// Optional rest param route "/:x*" test |
| 64 | +const test6: isEqualsType< |
| 65 | + RoutePropsForPath<'/user/:id*'> , |
| 66 | + { params: { id?: string }, id?: string } |
| 67 | +> = true; |
| 68 | + |
| 69 | +// rest param should not be present |
| 70 | +const test6_error: isEqualsType< |
| 71 | + RoutePropsForPath<'/user/:id*'> , |
| 72 | + { params: { id: string }, rest: string } |
| 73 | +> = false; |
| 74 | + |
| 75 | +// Rest param route "/:x+" test |
| 76 | +const test7: isEqualsType< |
| 77 | + RoutePropsForPath<'/user/:id+'> , |
| 78 | + { params: { id: string }, id: string } |
| 79 | +> = true; |
| 80 | + |
| 81 | +// rest param should not be present |
| 82 | +const test7_error: isEqualsType< |
| 83 | + RoutePropsForPath<'/user/:id+'>, |
| 84 | + { params: { id: string }, id: string, rest: string } |
| 85 | +> = false; |
| 86 | + |
| 87 | +// Handles leading/trailing slashes test |
| 88 | +const test8: isEqualsType< |
| 89 | + RoutePropsForPath<'/about-late/:seg1/:seg2/'> , |
| 90 | + { params: { seg1: string; seg2: string }, seg1: string, seg2: string } |
| 91 | +> = true; |
| 92 | + |
| 93 | +// Multiple params test (from overwrite properties test) |
| 94 | +const test9: isEqualsType< |
| 95 | + RoutePropsForPath<'/:path/:query'> , |
| 96 | + { params: { path: string; query: string }, path: string, query: string } |
| 97 | +> = true; |
| 98 | + |
| 99 | +// Empty route test |
| 100 | +const test10: isEqualsType< |
| 101 | + RoutePropsForPath<''> , |
| 102 | + { params: {} } |
| 103 | +> = true; |
0 commit comments