From: Eduardo San Martin Morote Date: Wed, 29 Jun 2022 16:18:08 +0000 (+0200) Subject: feat(types): allow extending global types X-Git-Tag: v4.1.0~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=31aaf3b82aae5c553a0bacd52d0be1c602e0ab30;p=thirdparty%2Fvuejs%2Frouter.git feat(types): allow extending global types --- diff --git a/packages/router/src/config.ts b/packages/router/src/config.ts new file mode 100644 index 00000000..1da3f7f1 --- /dev/null +++ b/packages/router/src/config.ts @@ -0,0 +1,6 @@ +/** + * Allows customizing existing types of the router that are used globally like `$router`, ``, and `beforeRouteLeave()`. **ONLY FOR INTERNAL USAGE**. + * + * @internal + */ +export interface TypesConfig {} diff --git a/packages/router/src/globalExtensions.ts b/packages/router/src/globalExtensions.ts index 1a940463..ae9121c4 100644 --- a/packages/router/src/globalExtensions.ts +++ b/packages/router/src/globalExtensions.ts @@ -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 + beforeRouteEnter?: TypesConfig extends Record<'beforeRouteEnter', infer T> + ? T + : NavigationGuardWithThis /** * 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 } } diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index 3a5f738a..c5de6361 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -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'