RouteRecordRedirectOption,
NamedLocationMap,
ExtractNamedRoutes,
+ ExtractRoutes,
// route records
_RouteRecordBase,
RouteMeta,
/**
* Router instance
*/
-export interface Router {
+export interface Router<Options extends RouterOptions = RouterOptions> {
/**
* @internal
*/
/**
* 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.
*
* @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
let started: boolean | undefined
const installedApps = new Set<App>()
- const router: Router = {
+ const router: Router<Options> = {
currentRoute,
listening: true,
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
+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
? T & { name: never }
: { name: never; children: never }
+export type ExtractRoutes<T extends Router> = ExtractNamedRoutes<
+ T['options']['routes']
+>
+
/**
* Used to define typed named locations
* @example
-import { ExtractNamedRoutes, Router } from './index'
+import {
+ ExtractNamedRoutes,
+ Router,
+ ExtractRoutes,
+ createRouter,
+} from './index'
import { DefineComponent } from 'vue'
declare const Comp: DefineComponent
declare module './index' {
interface NamedLocationMap {
'my-other-path': {
- sss: number
+ id: string
}
}
}
router.push({
name: 'my-other-path',
params: {
- sss: 1,
+ id: '222',
// @ts-expect-error does not exist
- xxxx: '22',
+ nonExistent: '22',
},
})
// @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