Open History to see the last commit
1import { expectTypeOf, test } from 'vitest'
2import { createFileRoute, createRootRoute } from '../src'
3import type { Route } from '@tanstack/router-core'
4β
5declare module '@tanstack/router-core' {
6 interface FilebaseRouteOptionsInterface<
7 TRegister,
8 TParentRoute,
9 TId,
10 TPath,
11 TSearchValidator,
12 TParams,
13 TLoaderDeps,
14 TLoaderFn,
15 TRouterContext,
16 TRouteContextFn,
17 TBeforeLoadFn,
18 TRemountDepsFn,
19 TSSR,
20 TServerMiddlewares,
21 THandlers,
22 > {
23 server?: {
24 middleware?: TServerMiddlewares
25 }
26 }
27β
28 interface RouteTypes<
29 TRegister,
30 TParentRoute,
31 TPath,
32 TFullPath,
33 TCustomId,
34 TId,
35 TSearchValidator,
36 TParams,
37 TRouterContext,
38 TRouteContextFn,
39 TBeforeLoadFn,
40 TLoaderDeps,
41 TLoaderFn,
42 TChildren,
43 TFileRouteTypes,
44 TSSR,
45 TServerMiddlewares,
46 THandlers,
47 > {
48 middleware: TServerMiddlewares
49 }
50}
51β
52const rootRoute = createRootRoute()
53β
54const indexRoute = createFileRoute('/')()
55β
56const invoicesRoute = createFileRoute('/invoices')()
57β
58const invoiceRoute = createFileRoute('/invoices/$invoiceId')()
59β
60const postLayoutRoute = createFileRoute('/_postLayout')()
61β
62const postsRoute = createFileRoute('/_postLayout/posts')()
63β
64const postRoute = createFileRoute('/_postLayout/posts/$postId_')()
65β
66const protectedRoute = createFileRoute('/(auth)/protected')()
67β
68declare module '@tanstack/router-core' {
69 interface FileRoutesByPath {
70 '/': {
71 preLoaderRoute: typeof indexRoute
72 parentRoute: typeof rootRoute
73 id: string
74 fullPath: string
75 path: string
76 }
77 '/(auth)/protected': {
78 preLoaderRoute: typeof protectedRoute
79 parentRoute: typeof rootRoute
80 id: '/protected'
81 fullPath: '/protected'
82 path: '(auth)/protected'
83 }
84 '/invoices': {
85 preLoaderRoute: typeof invoicesRoute
86 parentRoute: typeof indexRoute
87 id: '/invoices'
88 fullPath: '/invoices'
89 path: 'invoices'
90 }
91 '/invoices/$invoiceId': {
92 preLoaderRoute: typeof invoiceRoute
93 parentRoute: typeof invoicesRoute
94 id: '/invoices/$invoiceId'
95 fullPath: '/invoices/$invoiceId'
96 path: '/$invoiceId'
97 }
98 '/_postLayout': {
99 preLoaderRoute: typeof postLayoutRoute
100 parentRoute: typeof rootRoute
101 id: string
102 fullPath: string
103 path: string
104 }
105 '/_postLayout/posts': {
106 preLoaderRoute: typeof postsRoute
107 parentRoute: typeof postLayoutRoute
108 id: '/_postLayout/posts'
109 fullPath: '/posts'
110 path: '/posts'
111 }
112 '/_postLayout/posts/$postId_': {
113 preLoaderRoute: typeof postRoute
114 parentRoute: typeof postsRoute
115 id: '/_postLayout/posts/$postId_'
116 fullPath: '/posts/$postId'
117 path: '/$postId_'
118 }
119 }
120}
121β
122test('when creating a file route with a static route', () => {
123 expectTypeOf<'/invoices'>(invoicesRoute.fullPath)
124 expectTypeOf<'/invoices'>(invoicesRoute.id)
125 expectTypeOf<'invoices'>(invoicesRoute.path)
126})
127β
128test('when creating a file route with params', () => {
129 expectTypeOf<'/invoices/$invoiceId'>(invoiceRoute.fullPath)
130 expectTypeOf<'/invoices/$invoiceId'>(invoiceRoute.id)
131 expectTypeOf<'/$invoiceId'>(invoiceRoute.path)
132})
133β
134test('when creating a layout route', () => {
135 expectTypeOf<'/posts'>(postsRoute.fullPath)
136 expectTypeOf<'/_postLayout/posts'>(postsRoute.id)
137 expectTypeOf<'/posts'>(postsRoute.path)
138})
139β
140test('when creating a _ suffix route', () => {
141 expectTypeOf<'/posts/$postId'>(postRoute.fullPath)
142 expectTypeOf<'/$postId_'>(postRoute.path)
143 expectTypeOf<'/_postLayout/posts/$postId_'>(postRoute.id)
144})
145β
146test('when creating a folder group', () => {
147 expectTypeOf<'/protected'>(protectedRoute.fullPath)
148 expectTypeOf<'(auth)/protected'>(protectedRoute.path)
149 expectTypeOf<'/protected'>(protectedRoute.id)
150})
151β
152test('when creating a file route with middleware options', () => {
153 type Middleware = { readonly middleware: 'middleware' }
154 const middleware = { middleware: 'middleware' } as const
155β
156 const route = createFileRoute('/invoices')({
157 server: {
158 middleware: [middleware],
159 },
160 })
161β
162 type ExtractMiddlewares<TRoute> =
163 TRoute extends Route<
164 any,
165 any,
166 any,
167 any,
168 any,
169 any,
170 any,
171 any,
172 any,
173 any,
174 any,
175 any,
176 any,
177 any,
178 any,
179 any,
180 infer TMiddlewares,
181 any
182 >
183 ? TMiddlewares
184 : never
185β
186 expectTypeOf<ExtractMiddlewares<typeof route>>().toEqualTypeOf<
187 readonly [Middleware]
188 >()
189})