]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
improvements
authorpikax <carlos@hypermob.co.uk>
Fri, 9 Apr 2021 12:17:23 +0000 (13:17 +0100)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 30 Jun 2022 07:59:00 +0000 (09:59 +0200)
src/types/named.ts
test-dts/namedRoutes.test-d.ts

index e3d83264b1a9d6eb8f57c30a48761ecde2f40cba..c478605d020ed2191ce2e35cdf5c23a0cbe4ad17 100644 (file)
@@ -7,35 +7,6 @@ export type ExtractNamedRoutes<T> = [T] extends [ReadonlyArray<infer U>]
         ? 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> {
index 6ad366d6e7c7bb4dfad9b767df5ebf71b0635e59..d7fbf19d50b94c6349b7f780391c5d4629142f08 100644 (file)
@@ -8,11 +8,15 @@ const routes = [
     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',
@@ -22,39 +26,35 @@ const routes = [
         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
     }
   }
 }
@@ -66,8 +66,8 @@ router.push({
   params: {
     sss: 1,
     // @ts-expect-error does not exist
-    xxxx: '22'
-  }
+    xxxx: '22',
+  },
 })
 
 router.push({