]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
add comment
authorpikax <carlos@hypermob.co.uk>
Fri, 9 Apr 2021 15:39:50 +0000 (16:39 +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 bf9097d06eee829c178ad3853f6d82420f1692c9..d9e0ef05799ee95dd2e6d2738af57a79e985a51c 100644 (file)
@@ -1,5 +1,7 @@
-import { RouteRecordRaw, _RouteRecordBase } from '.'
-
+/**
+ * This will flat the routes into an object with `key` === `router.name`
+ * and the value will be `unknown` since we don't have way to describe params types
+ */
 export type ExtractNamedRoutes<T> = [T] extends [ReadonlyArray<infer U>]
   ? ExtractNamedRoutes<RouteFix<U>>
   : ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : {}) &
@@ -13,70 +15,25 @@ export type ExtractNamedRoutes<T> = [T] extends [ReadonlyArray<infer U>]
 type RouteFix<T> = T extends { name: string; children: any }
   ? T
   : T extends { name: string }
-  ? T & { children: never[] }
+  ? T & { children: never }
   : T extends { children: any }
-  ? T & { name: '' }
-  : { name: ''; children: never }
-
-// // declare const xxx: NamedRoutes<
-// //   | {
-// //       name: 'LOL'
-// //     }
-// //   | { name: 'xxx' }
-// //   | { children: {} }
-// // >
-// // xxx.name
-
-// declare const typed: ExtractNamedRoutes<
-//   [
-//     {
-//       path: 'my-path'
-//       name: 'test'
-//       // children must be declared :(
-//       // children: []
-//     },
-//     {
-//       path: 'my-path'
-//       name: 'my-other-path'
-//       // children must be declared :(
-//       // children: []
-//     },
-//     {
-//       path: 'random'
-//       name: 'tt'
-//       children: [
-//         {
-//           path: 'random-child'
-//           name: 'random-child'
-//           // children: []
-//         }
-//       ]
-//     },
-//     {
-//       name: '1'
-//       children: [
-//         {
-//           name: '2'
-//           children: [{ name: '3'; children: [{ name: '4' }] }]
-//         }
-//       ]
-//     }
-//   ]
-// >
-
-// typed['my-other-path']
-// typed['random-child']
-// typed.test
-// typed.tt
-// typed[1]
-// typed[2]
-// typed[3]
-// typed[4]
-
-export function defineRoutes<
-  T extends Array<RouteRecordRaw | Readonly<RouteRecordRaw>>
->(routes: T): ExtractNamedRoutes<T> {
-  return routes as any
-}
+  ? T & { name: never }
+  : { name: never; children: never }
 
+/**
+ * Used to define typed named locations
+ * @example
+ * ```ts
+ * declare module 'vue-router' {
+ *   interface NamedLocationMap {
+ *    // 'home' no params
+ *    home: {}
+ *    // 'product' `{id: string}` required parameter
+ *    product: {
+ *      id: string
+ *    }
+ *   }
+ * }
+ * ```
+ */
 export interface NamedLocationMap {}
index 83bd8688f3f5e4dc835e64285a61b8c91be76920..577040a15ec9c2d28526912d694ba6b7d08fbf0c 100644 (file)
@@ -30,7 +30,12 @@ const routes = [
     children: [
       {
         name: '2',
-        children: [{ name: '3', children: [{ name: '4' }] }],
+        children: [
+          {
+            name: '3',
+            children: [{ name: '4' }, { path: '', children: [{ name: '5' }] }],
+          },
+        ],
       },
     ],
   },
@@ -46,6 +51,7 @@ typed[1]
 typed[2]
 typed[3]
 typed[4]
+typed[5]
 //@ts-expect-error
 typed['non-existing']