]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
chore: add createRouter templated options
authorpikax <carlos@hypermob.co.uk>
Wed, 21 Apr 2021 09:59:19 +0000 (10:59 +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/router.ts
src/types/index.ts
src/types/named.ts
test-dts/namedRoutes.test-d.ts

index 83ac531e23655cc5abda75c6b411a3c17a2d19c4..46ead1add30517d4c295d20e2c11d9dd77519901 100644 (file)
@@ -49,6 +49,7 @@ export type {
   RouteRecordRedirectOption,
   NamedLocationMap,
   ExtractNamedRoutes,
+  ExtractRoutes,
   // route records
   _RouteRecordBase,
   RouteMeta,
index 68b0da8fb57cb72eaadeb9a9f2f77e5f44cc2f30..76c720b7833c925eb01f89d047b3eadd242871ba 100644 (file)
@@ -185,7 +185,7 @@ export interface RouterOptions extends PathParserOptions {
 /**
  * Router instance
  */
-export interface Router {
+export interface Router<Options extends RouterOptions = RouterOptions> {
   /**
    * @internal
    */
@@ -197,7 +197,7 @@ export interface Router {
   /**
    * Original options object passed to create the Router
    */
-  readonly options: RouterOptions
+  readonly options: Options
 
   /**
    * Allows turning off the listening of history events. This is a low level api for micro-frontends.
@@ -360,7 +360,9 @@ export interface Router {
  *
  * @param options - {@link RouterOptions}
  */
-export function createRouter(options: RouterOptions): Router {
+export function createRouter<Options extends RouterOptions>(
+  options: Options
+): Router<Options> {
   const matcher = createRouterMatcher(options.routes, options)
   const parseQuery = options.parseQuery || originalParseQuery
   const stringifyQuery = options.stringifyQuery || originalStringifyQuery
@@ -1157,7 +1159,7 @@ export function createRouter(options: RouterOptions): Router {
   let started: boolean | undefined
   const installedApps = new Set<App>()
 
-  const router: Router = {
+  const router: Router<Options> = {
     currentRoute,
     listening: true,
 
index 85a60deb8c9e8e45d855151de5f47a6111d94b43..7ececb4c24bd9ae2b171cad7729d0b9cac8cd647 100644 (file)
@@ -6,7 +6,7 @@ import { HistoryState } from '../history/common'
 import { NavigationFailure } from '../errors'
 import { NamedLocationMap } from './named'
 
-export { NamedLocationMap, ExtractNamedRoutes } from './named'
+export { NamedLocationMap, ExtractNamedRoutes, ExtractRoutes } from './named'
 
 export type Lazy<T> = () => Promise<T>
 export type Override<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
index d9e0ef05799ee95dd2e6d2738af57a79e985a51c..d238da59a81a64b1df1fce993d51d9d5eb2d9120 100644 (file)
@@ -1,3 +1,5 @@
+import { Router } from '../router'
+
 /**
  * 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
@@ -20,6 +22,10 @@ type RouteFix<T> = T extends { name: string; children: any }
   ? T & { name: never }
   : { name: never; children: never }
 
+export type ExtractRoutes<T extends Router> = ExtractNamedRoutes<
+  T['options']['routes']
+>
+
 /**
  * Used to define typed named locations
  * @example
index 577040a15ec9c2d28526912d694ba6b7d08fbf0c..5fea37fac5c0d2d1fd7eb6022d215bb6e79a8cef 100644 (file)
@@ -1,4 +1,9 @@
-import { ExtractNamedRoutes, Router } from './index'
+import {
+  ExtractNamedRoutes,
+  Router,
+  ExtractRoutes,
+  createRouter,
+} from './index'
 import { DefineComponent } from 'vue'
 
 declare const Comp: DefineComponent
@@ -58,7 +63,7 @@ typed['non-existing']
 declare module './index' {
   interface NamedLocationMap {
     'my-other-path': {
-      sss: number
+      id: string
     }
   }
 }
@@ -68,9 +73,9 @@ declare const router: Router
 router.push({
   name: 'my-other-path',
   params: {
-    sss: 1,
+    id: '222',
     // @ts-expect-error does not exist
-    xxxx: '22',
+    nonExistent: '22',
   },
 })
 
@@ -78,3 +83,14 @@ router.push({
   // @ts-expect-error location name does not exist
   name: 'random-location',
 })
+
+const otherRouter = createRouter({
+  history: {} as any,
+  routes: [{ path: 'e', name: 'test', component: Comp }] as const,
+})
+
+declare const otherRoutes: ExtractRoutes<typeof otherRouter>
+
+otherRoutes.test
+// @ts-expect-error
+otherRoutes.test2