? ExtractNamedRoutes<T['children']>
: {})
-// declare const test: ExtractNamedRoutes<
-// [
-// {
-// path: 'my-path'
-// name: 'test'
-// children: []
-// },
-// {
-// path: 'my-path'
-// name: 'my-other-path'
-// // children: []
-// },
-// {
-// path: 'random'
-// name: 'tt'
-// children: [
-// {
-// path: 'random-child'
-// name: 'random-child'
-// }
-// ]
-// }
-// ]
-// >
-// test
-// test['my-other-path']
-// test.test, test.tt
-// test['random-child']
-
export function defineRoutes<
T extends Array<RouteRecordRaw | Readonly<RouteRecordRaw>>
>(routes: T): ExtractNamedRoutes<T> {
path: 'my-path',
name: 'test',
component: Comp,
+ // children must be declared :(
+ children: [],
},
{
path: 'my-path',
name: 'my-other-path',
component: Comp,
+ // children must be declared :(
+ children: [],
},
{
path: 'random',
path: 'random-child',
name: 'random-child',
component: Comp,
+ children: [],
},
],
},
-]
-
-type TypedRoutes = ExtractNamedRoutes< [
- {
- path: 'my-path',
- name: 'test',
- // component: ,
- },
- {
- path: 'my-path',
- name: 'my-other-path',
- // component: Comp,
- },
{
- path: 'random',
- name: 'tt',
+ name: '1',
children: [
{
- path: 'random-child',
- name: 'random-child',
- component: {},
+ name: '2',
+ children: [{ name: '3' }],
},
],
},
-]>
+] as const
+
+declare const typed: ExtractNamedRoutes<typeof routes>
+
+typed['my-other-path']
+typed['random-child']
+typed.test
+typed.tt
+typed[1]
+typed[2]
+typed[3]
declare module './index' {
- interface NamedLocationMap {
+ interface NamedLocationMap {
'my-other-path': {
- sss: number
+ sss: number
}
}
}
params: {
sss: 1,
// @ts-expect-error does not exist
- xxxx: '22'
- }
+ xxxx: '22',
+ },
})
router.push({