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

index d5975392bc29c63acae9c4076ba2ce403a7e87bc..34ba620eb962e6dd8d585738c037816c0dee4a95 100644 (file)
@@ -47,7 +47,6 @@ export type {
   RouteLocationMatched,
   RouteLocationOptions,
   RouteRecordRedirectOption,
-  RouteNamedLocation,
   defineRoutes,
   NamedLocationMap,
   ExtractNamedRoutes,
index 6d313ab238c5af54297d8303f953111e58884b6c..21abc29bae053e21f80c0a7e310482db492a97e7 100644 (file)
@@ -6,12 +6,7 @@ import { HistoryState } from '../history/common'
 import { NavigationFailure } from '../errors'
 import { NamedLocationMap } from './named'
 
-export {
-  RouteNamedLocation,
-  NamedLocationMap,
-  defineRoutes,
-  ExtractNamedRoutes,
-} from './named'
+export { NamedLocationMap, defineRoutes, ExtractNamedRoutes } from './named'
 
 export type Lazy<T> = () => Promise<T>
 export type Override<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
index 750febe23c5da7472678c2d771efed6ff03b7527..e3d83264b1a9d6eb8f57c30a48761ecde2f40cba 100644 (file)
-import { RouteLocationOptions, RouteRecordRaw, _RouteRecordBase } from '.'
+import { RouteRecordRaw, _RouteRecordBase } from '.'
 
-// export type ExtractNameRoute<T extends Readonly<RouteRecordRaw>> =
-//   | ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : never)
-//   | ([T] extends [{ children: Readonly<RouteRecordRaw[]> }]
-//       ? ExtractNamedRoutes<T['children']>
-//       : never)
-
-export type ExtractNamedRoute<T extends Readonly<_RouteRecordBase>> = [
-  T
-] extends [{ name: string; readonly children?: any[] }]
-  ? { [K in T['name']]: unknown } &
-      ([T['children']] extends [Readonly<Array<_RouteRecordBase>>]
+export type ExtractNamedRoutes<T> = [T] extends [ReadonlyArray<infer U>]
+  ? ExtractNamedRoutes<U>
+  : ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : {}) &
+      ([T] extends [{ children?: undefined | unknown | any }]
         ? ExtractNamedRoutes<T['children']>
         : {})
-  : never
-
-export type ExtractNamedRoutes<
-  T extends Readonly<Array<_RouteRecordBase>> | undefined
-> = T extends Readonly<Array<infer R>>
-  ? // ? [R] extends [{ name: string /*params?: infer Params*/ }]
-    [R] extends [_RouteRecordBase]
-    ? ExtractNamedRoute<R>
-    : {}
-  : never
-
-// const routes = [
-//   {
-//     path: 'my-path',
-//     name: 'test',
-//     component: Comp,
-//   },
-//   {
-//     path: 'my-path',
-//     name: 'my-other-path',
-//     component: Comp,
-//   },
-//   {
-//     path: 'random',
-//     name: 'tt',
-//     children: [
-//       {
-//         path: 'random-child',
-//         name: 'random-child',
-//         component: Comp,
-//       },
-//     ],
-//   },
-// ] as const
-
-// type TypedRoutes = ExtractNamedRoutes<typeof routes>
-
-// declare const Comp: () => any
 
-// const routes = [
-//   {
-//     path: 'my-path',
-//     name: 'test',
-//     component: Comp,
-//   },
-//   {
-//     path: 'my-path',
-//     // name: 'my-other-path',
-//     component: Comp,
-//   },
-// ] as const
-
-// type XXX = ExtractNamedRoutes<
-//   Readonly<
-//     [
-//       {
-//         path: 'ttt'
-//         name: 'sddsd'
-//         component: any
-//         children: [
-//           {
-//             path: 'ttt'
-//             name: 'child'
-//             component: any
-//           },
-//           {
-//             path: 'ttt'
-//             name: 'child-other'
-//             component: any
-//           }
-//         ]
-//       }
-//     ]
-//   >
+// 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'
+//         }
+//       ]
+//     }
+//   ]
 // >
-
-// interface XXW extends XXX {}
-
-// const xxx2: XXW
-// type TypedRoutes = ExtractNamedRoutes<typeof routes>
-
-//   export type ExtractNamedRoutes<
-//   T extends Array<RouteRecordRaw> | Readonly<Array<RouteRecordRaw>>
-// > = T extends Array<infer R>
-//   ? [R] extends [{ name: string /*params?: infer Params*/ }]
-//     ? {
-//         [K in R['name']]: unknown /*TODO add params*/ /*R['params'] extends Params ? Params : Params*/
-//       }
-//     : never
-//   : T extends Readonly<Array<infer R>>
-//   ? [R] extends [{ name: string /*params?: infer Params*/ }]
-//     ? {
-//         [K in R['name']]: unknown /*TODO add params*/ /*R['params'] extends Params ? Params : Params*/
-//       }
-//     : never
-//   : never
+// 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> {
   return routes as any
 }
-export interface NamedLocationMap {}
-
-export interface RouteNamedLocation<
-  T extends keyof NamedLocationMap = keyof NamedLocationMap
-> extends RouteLocationOptions {
-  name: T
-  // params: NamedLocationMap[T]
-}
 
-// declare const r: [
-//   {
-//     name: 'test'
-//     params: {
-//       number: 1
-//     }
-//   },
-//   {
-//     name: 'LOL'
-//     params: {
-//       sss: 'sss'
-//     }
-//   },
-//   {
-//     name: 'other'
-//   },
-//   {
-//     path: 'ssss'
-//   }
-// ]
-
-// declare const x: ExtractNamedRoutes<typeof r>
+export interface NamedLocationMap {}
index ffe62f106afe07d1e173c50dd967b544e83ec5f1..6ad366d6e7c7bb4dfad9b767df5ebf71b0635e59 100644 (file)
@@ -25,14 +25,36 @@ const routes = [
       },
     ],
   },
-] as const
+]
 
-type TypedRoutes = ExtractNamedRoutes<typeof routes>
+type TypedRoutes = ExtractNamedRoutes< [
+  {
+    path: 'my-path',
+    name: 'test',
+    // component: ,
+  },
+  {
+    path: 'my-path',
+    name: 'my-other-path',
+    // component: Comp,
+  },
+  {
+    path: 'random',
+    name: 'tt',
+    children: [
+      {
+        path: 'random-child',
+        name: 'random-child',
+        component: {},
+      },
+    ],
+  },
+]>
 
 declare module './index' {
-  interface NamedLocationMap {
+  interface NamedLocationMap  {
     'my-other-path': {
-      sss: number
+      sss:  number
     }
   }
 }
@@ -43,9 +65,9 @@ router.push({
   name: 'my-other-path',
   params: {
     sss: 1,
-    // @ts-expect-error
-    xxxx: '22',
-  },
+    // @ts-expect-error does not exist
+    xxxx: '22'
+  }
 })
 
 router.push({