Skip to content

Commit 1cd93b3

Browse files
committed
feat: allow overriding the global Router type
1 parent f405f64 commit 1cd93b3

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

packages/router/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Allows customizing existing types of the router that are used globally like `$router`, `<RouterLink>`, etc. **ONLY FOR INTERNAL USAGE**.
33
*
4+
* - `Router` - swaps the public {@link Router} type (e.g. to `EXPERIMENTAL_Router`)
45
* - `$router` - the router instance
56
* - `$route` - the current route location
67
* - `beforeRouteEnter` - Page component option

packages/router/src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export type {
144144
} from './typed-routes'
145145

146146
export { createRouter } from './router'
147-
export type { Router, RouterOptions } from './router'
147+
export type { RouterOptions } from './router'
148+
export type { Router as _RouterClassic } from './router'
148149
export type { RouterScrollBehavior } from './scrollBehavior'
149150

150151
export { NavigationFailureType, isNavigationFailure } from './errors'
@@ -174,7 +175,7 @@ export * from './useApi'
174175

175176
// Global extensions for Vue
176177
import type { TypesConfig } from './config'
177-
import type { Router } from './router'
178+
import type { Router as RouterClassic } from './router'
178179
import type { RouterLink } from './RouterLink'
179180
import type { RouterView } from './RouterView'
180181
import type {
@@ -183,6 +184,25 @@ import type {
183184
RouteLocationNormalizedLoaded,
184185
} from './typed-routes'
185186

187+
/**
188+
* Router instance.
189+
*
190+
* By default this resolves to the classic {@link RouterClassic}. Augment
191+
* {@link TypesConfig} with a `Router` slot to swap the public type — for
192+
* example to the experimental router:
193+
*
194+
* ```ts
195+
* import type { EXPERIMENTAL_Router } from 'vue-router/experimental'
196+
* declare module 'vue-router' {
197+
* interface TypesConfig {
198+
* Router: EXPERIMENTAL_Router
199+
* }
200+
* }
201+
* ```
202+
*/
203+
export type Router =
204+
TypesConfig extends Record<'Router', infer T> ? T : RouterClassic
205+
186206
declare module 'vue' {
187207
export interface ComponentCustomOptions {
188208
/**

packages/router/src/router.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,8 @@ export function createRouter(options: RouterOptions): Router {
10131013
app.component('RouterLink', RouterLink)
10141014
app.component('RouterView', RouterView)
10151015

1016+
// augmented to the experimental shape it diverges from `createRouter`'s
1017+
// return type. FIXME.
10161018
app.config.globalProperties.$router = router as Router
10171019
Object.defineProperty(app.config.globalProperties, '$route', {
10181020
enumerable: true,

packages/router/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"outDir": "dist",
1818
"sourceMap": false,
1919
"noEmit": true,
20+
"composite": true,
2021
"target": "esnext",
2122
"module": "esnext",
2223
"moduleResolution": "Bundler",

0 commit comments

Comments
 (0)