]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(types): allow extending global types
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 29 Jun 2022 16:18:08 +0000 (18:18 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 30 Jun 2022 07:59:00 +0000 (09:59 +0200)
packages/router/src/config.ts [new file with mode: 0644]
packages/router/src/globalExtensions.ts
packages/router/src/index.ts

diff --git a/packages/router/src/config.ts b/packages/router/src/config.ts
new file mode 100644 (file)
index 0000000..1da3f7f
--- /dev/null
@@ -0,0 +1,6 @@
+/**
+ * Allows customizing existing types of the router that are used globally like `$router`, `<RouterLink>`, and `beforeRouteLeave()`. **ONLY FOR INTERNAL USAGE**.
+ *
+ * @internal
+ */
+export interface TypesConfig {}
index 1a940463444987bef85a57537bffd30e372a0212..ae9121c48b3d94c56610bad02683fd7154e4a2e3 100644 (file)
@@ -6,6 +6,7 @@ import type {
 import { RouterView } from './RouterView'
 import { RouterLink } from './RouterLink'
 import type { Router } from './router'
+import type { TypesConfig } from './config'
 
 declare module '@vue/runtime-core' {
   export interface ComponentCustomOptions {
@@ -21,7 +22,9 @@ declare module '@vue/runtime-core' {
      * @param next - function to validate, cancel or modify (by redirecting) the
      * navigation
      */
-    beforeRouteEnter?: NavigationGuardWithThis<undefined>
+    beforeRouteEnter?: TypesConfig extends Record<'beforeRouteEnter', infer T>
+      ? T
+      : NavigationGuardWithThis<undefined>
 
     /**
      * Guard called whenever the route that renders this component has changed but
@@ -33,7 +36,9 @@ declare module '@vue/runtime-core' {
      * @param next - function to validate, cancel or modify (by redirecting) the
      * navigation
      */
-    beforeRouteUpdate?: NavigationGuard
+    beforeRouteUpdate?: TypesConfig extends Record<'beforeRouteUpdate', infer T>
+      ? T
+      : NavigationGuard
 
     /**
      * Guard called when the router is navigating away from the current route that
@@ -44,22 +49,30 @@ declare module '@vue/runtime-core' {
      * @param next - function to validate, cancel or modify (by redirecting) the
      * navigation
      */
-    beforeRouteLeave?: NavigationGuard
+    beforeRouteLeave?: TypesConfig extends Record<'beforeRouteLeave', infer T>
+      ? T
+      : NavigationGuard
   }
 
   export interface ComponentCustomProperties {
     /**
      * Normalized current location. See {@link RouteLocationNormalizedLoaded}.
      */
-    $route: RouteLocationNormalizedLoaded
+    $route: TypesConfig extends Record<'$route', infer T>
+      ? T
+      : RouteLocationNormalizedLoaded
     /**
      * {@link Router} instance used by the application.
      */
-    $router: Router
+    $router: TypesConfig extends Record<'$router', infer T> ? T : Router
   }
 
   export interface GlobalComponents {
-    RouterView: typeof RouterView
-    RouterLink: typeof RouterLink
+    RouterView: TypesConfig extends Record<'RouterView', infer T>
+      ? T
+      : typeof RouterView
+    RouterLink: TypesConfig extends Record<'RouterLink', infer T>
+      ? T
+      : typeof RouterLink
   }
 }
index 3a5f738a7f98af7d6f4c01815f4197e86486d56d..c5de6361d64351c4101cfb5c7183f24f15a2e3ff 100644 (file)
@@ -86,6 +86,8 @@ export type {
 export { RouterView } from './RouterView'
 export type { RouterViewProps } from './RouterView'
 
+export type { TypesConfig } from './config'
+
 export * from './useApi'
 
 export * from './globalExtensions'